Note that there are some explanatory texts on larger screens.

plurals
  1. POCore Data NSFetchrequest integer
    primarykey
    data
    text
    <p>I have the following scenario. I have an app that handles data by using Core Data. I have an entity called "Brothers" which has 3 attributes: Name, Status, Age.</p> <p>lets say that I have 1 record on my database that has:</p> <pre><code>-Name ==&gt; Joe (String) -Status ==&gt; Married (String) -Age ==&gt; 28 (Integer) </code></pre> <p>I have an UIlabel a UItextfield and a Button. I want to be able to type the name (in this case Joe) on the UItextfield press the button and display the age (in this case 28) in the UILabel. I would also like to store the age value into a variable type integer so I can make some calculations with it later on.</p> <p>below the code that I have inside the button.</p> <pre><code>NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Brothers" inManagedObjectContext:context]; NSFetchRequest *request = [[NSFetchRequest alloc]init]; [request setEntity:entitydesc]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age %d", [self.age.text integerValue]]; [request setPredicate:predicate]; NSError *error; NSArray *integer = [context executeFetchRequest:request error:&amp;error]; self.displayLabel.text = integer; </code></pre> <p><strong>Update #1</strong></p> <p>I updated the code that i have inside the button and now i am able to search by the name and display the age. I'm still looking into storing the age as an integer into a variable so i can use it later on.</p> <pre><code>NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Brothers" inManagedObjectContext:context]; NSFetchRequest *request = [[NSFetchRequest alloc]init]; [request setEntity:entitydesc]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstname like %@", self.firstnameTextField.text]; [request setPredicate:predicate]; NSError *error; NSArray *integer = [context executeFetchRequest:request error:&amp;error]; if(integer.count &lt;= 0){ self.displayLabel.text = @"No records found"; } else { NSString *age; for (NSManagedObject *object in integer) { age = [object valueForKey:@"age"]; } self.displayLabel.text = [NSString stringWithFormat:@"age: %@",age]; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload