ios - Core Data NSFetchrequest integer -
i have following scenario. have app handles data using core data. have entity called "brothers" has 3 attributes: name, status, age.
lets have 1 record on database has:
-name ==> joe (string) -status ==> married (string) -age ==> 28 (integer)
i have uilabel uitextfield , button. want able type name (in case joe) on uitextfield press button , display age (in case 28) in uilabel. store age value variable type integer can make calculations later on.
below code have inside button.
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:&error]; self.displaylabel.text = integer;
update #1
i updated code have inside button , able search name , display age. i'm still looking storing age integer variable can use later on.
nsentitydescription *entitydesc = [nsentitydescription entityforname:@"brothers" inmanagedobjectcontext:context]; nsfetchrequest *request = [[nsfetchrequest alloc]init]; [request setentity:entitydesc]; nspredicate *predicate = [nspredicate predicatewithformat:@"firstname %@", self.firstnametextfield.text]; [request setpredicate:predicate]; nserror *error; nsarray *integer = [context executefetchrequest:request error:&error]; if(integer.count <= 0){ self.displaylabel.text = @"no records found"; } else { nsstring *age; (nsmanagedobject *object in integer) { age = [object valueforkey:@"age"]; } self.displaylabel.text = [nsstring stringwithformat:@"age: %@",age]; } }
a predicate expression. if expression evaluates true predicate satisfied. if searching age you'd use e.g.
[nspredicate predicatewithformat:@"age = %d", [self.age.text integervalue]]
or name:
[nspredicate predicatewithformat:@"name = %@", somenameorother]
or both:
[nspredicate predicatewithformat:@"(name = %@) , (age = %d)", somenameorother, [self.age.text integervalue]]
a fetch request gets actual nsmanagedobject
s. you'd array of brother
s. therefore want more output name:
if([array count]) self.displaylabel.text = [array[0] name];
or age:
... self.displaylabel.text = [[array[0] age] stringvalue];
or whatever other property you're outputting.
Comments
Post a Comment