我使用ASIHTTPRequest
解析數據。現在,我想將這個 數據存儲在覈心數據中。使用this API。 我的實體名稱短信,包括三個ID,名稱和IsActive。 此代碼解析數據。如何在覈心數據中添加JSON解析數據?
-(void)DataRetireve
{
deatilinfoarray = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:
@"http://sms.instatalkcommunications.com/apireq/GetSMSCategories?
t=1&h=admin¶m=1"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];``
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSError *error;
NSString *responseString = nil;
if (!error)
{
responseString = [request responseString];
SBJsonParser *parser = [[SBJsonParser alloc] init] ;
NSMutableArray *jsondata = [parser objectWithString:responseString];
NSMutableArray *jsondata1 = [[NSMutableArray alloc]init];
info *myinfo=[[info alloc]init];
for(int i=0;i<jsondata.count;i++)
{
[email protected]"Id";
[email protected]"Name";
[email protected]"IsActive";
[jsondata1 addObject:myinfo];
NSLog(@"%@",responseString);
}
for(int i=0;i<jsondata1.count;i++)
{
myinfo= [jsondata1 objectAtIndex:i];
NSManagedObjectContext *context=[self managedObjectContext];
NSManagedObject *sms = [NSEntityDescription insertNewObjectForEntityForName:@"SMS" inManagedObjectContext:context];
NSLog(@"%@", context);
[sms setValue:@"Id" forKey:@"Id"];
[sms setValue:@"Name" forKey:@"Name"];
[sms setValue:@"IsActive" forKey:@"IsActive"];
[jsondata1 addObject:myinfo];
}
完成代碼這一點,但給我的錯誤
當然,用這行代碼NSManagedObjectContext * context;您沒有有效的託管對象上下文。從AppDelegate獲取權限或將其傳遞給您的相關ViewController。 – Matz 2014-10-02 07:18:21
我通過這段代碼 - (NSManagedObjectContext *)managedObjectContext { NSManagedObjectContext * context = nil; id delegate = [[UIApplication sharedApplication] delegate]; if([delegate performSelector:@selector(managedObjectContext)]) { context = [delegate managedObjectContext]; } return context; } – 2014-10-02 08:03:18
給我看這個錯誤int main(int argc,char * argv []){ @autoreleasepool { return UIApplicationMain(argc,argv,nil,NSStringFromClass([AppDelegate class])); } } – 2014-10-02 08:05:56