ios - sorting entity in coredata to textView? -
i @ loss, i'm using core data record guests @ event; working fine guest entity, , 3 attributes, lastname, name , zipcode. in order show user guests displaying guests in non editable textview, want display guests in alphabetical order in textview since listed order of time entered app. here code pausing problem :
- (void)viewdidload { [self drawtext]; [super viewdidload]; appdelegate *appdelegate = (appdelegate*)[[uiapplication sharedapplication] delegate]; nsfetchrequest *request = [[nsfetchrequest alloc]init]; nsentitydescription *entity = [nsentitydescription entityforname:@"guest" inmanagedobjectcontext:appdelegate.managedobjectcontext]; [request setentity:entity]; nsmanagedobjectcontext *context = [appdelegate managedobjectcontext]; nserror *error; nsarray *fetchedobjects = [context executefetchrequest:request error:&error]; nsstring*gueststring =@""; for(nsmanagedobject *obj in fetchedobjects) { gueststring =[gueststring stringbyappendingstring:[nsstring stringwithformat:@"%@ %@ %@\n",[obj valueforkey:@"guestlastname" ],[obj valueforkey:@"guestname"],[obj valueforkey:@"guestzipcode"]; } self.textview.text = gueststring; for(nsmanagedobject *obj in fetchedobjects) { nslog(@"name:%@\n last name %@\n" , [obj valueforkey:@"guestname"],[obj valueforkey:@"guestlastname"]); } }
any appreciated since trying sorts thing out no avail ? thanking in advance.
try this..
- (void)viewdidload { [self drawtext]; [super viewdidload]; appdelegate *appdelegate = (appdelegate*)[[uiapplication sharedapplication] delegate]; nsfetchrequest *request = [[nsfetchrequest alloc]init]; nsentitydescription *entity = [nsentitydescription entityforname:@"guest" inmanagedobjectcontext:appdelegate.managedobjectcontext]; [request setentity:entity]; nsmanagedobjectcontext *context = [appdelegate managedobjectcontext]; nserror *error; nsarray *fetchedobjects = [[[context executefetchrequest:request error:&error]sortedarrayusingdescriptors:[nsarray arraywithobject:[nssortdescriptor sortdescriptorwithkey:@"guestname" ascending:yes]]mutablecopy]; nsstring*gueststring =@""; for(nsmanagedobject *obj in fetchedobjects) { gueststring =[gueststring stringbyappendingstring:[nsstring stringwithformat:@"%@ %@ %@\n",[obj valueforkey:@"guestlastname" ],[obj valueforkey:@"guestname"],[obj valueforkey:@"guestzipcode"]; } self.textview.text = gueststring; for(nsmanagedobject *obj in fetchedobjects) { nslog(@"name:%@\n last name %@\n" , [obj valueforkey:@"guestname"],[obj valueforkey:@"guestlastname"]); } }
Comments
Post a Comment