我使用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