Xcode将可变字典从JSON添加到可变数组
我有一个可变的数组故事,我试图向其中添加一个JSON响应的副本,但它会生成0个故事,而数组中没有任何内容。我做错了什么?JSON对象的格式正确,我可以单独显示每个组件,但它们不会添加到数组中Xcode将可变字典从JSON添加到可变数组,json,xcode,nsmutablearray,nsmutabledictionary,Json,Xcode,Nsmutablearray,Nsmutabledictionary,我有一个可变的数组故事,我试图向其中添加一个JSON响应的副本,但它会生成0个故事,而数组中没有任何内容。我做错了什么?JSON对象的格式正确,我可以单独显示每个组件,但它们不会添加到数组中 NSString *str=@"http://www.foo.com/some.php"; NSURL *url=[NSURL URLWithString:str]; NSData *data=[NSData dataWithContentsOfURL:url]; NSError *error=nil; N
NSString *str=@"http://www.foo.com/some.php";
NSURL *url=[NSURL URLWithString:str];
NSData *data=[NSData dataWithContentsOfURL:url];
NSError *error=nil;
NSMutableDictionary *response=[NSJSONSerialization JSONObjectWithData:data options:
NSJSONReadingMutableContainers error:&error];
NSLog(@"Your JSON Object: %@", response);
NSLog(@"Location: %@", response[@"location"]);
NSLog(@"Service ID: %@", response[@"serviceID"]);
NSLog(@"Problem: %@", response[@"problem"]);
[stories addObject:[response copy] ];
NSLog(@"Workorders: %d", [stories count]);
NSLog(@"WO Defined: %@",stories);
好了,试试这个
NSMutableArray *stories = [NSMutableArray array];
当您使用web浏览器访问页面时,您能给出一个JSON的示例吗?您能解释一下如何初始化可变数组:
stories
Sorry,它位于文件NSMutableArray*stories的顶部;看起来故事没有初始化。因为Objective C允许向零对象发送消息,所以在调用[stories addObject:[response copy]]时不会遇到任何问题。