NSMutable 数组错误
我有这段代码
SBJsonParser *vol= [[SBJsonParser alloc]init];
if (body) {
NSArray *feeds = [vol objectWithString:body error:nil];
NSDictionary *results = [body JSONValue];
NSArray *subs = [results valueForKey:@"subscriptions"];
NSArray *list;
NSMutableArray *sub;
for (NSDictionary *iscrizione in subs){
NSLog([iscrizione objectForKey:@"title"]);
NSLog([iscrizione objectForKey:@"htmlUrl"]);
list=[NSArray arrayWithObjects:[iscrizione objectForKey:@"title"], [iscrizione objectForKey:@"htmlUrl"], nil];
[sub addObject:list];
}
,但是当我尝试将列表添加到 NSMutableArray 时,程序崩溃了。为什么? 如何插入 [iscrizione objectForKey:@"title"]
和 [iscrizione objectForKey:@"htmlUrl"]
在数据结构中?
编辑**
SBJsonParser *vol= [[SBJsonParser alloc]init];
if (body) {
NSArray *feeds = [vol objectWithString:body error:nil];
NSDictionary *results = [body JSONValue];
NSArray *subs = [results valueForKey:@"subscriptions"];
NSMutableArray *sub = [[NSMutableArray alloc] initWithCapacity:[subs count]];
for (NSDictionary *iscrizione in subs){
[sub addObject:iscrizione];
}
NSDictionary *it=[[NSDictionary alloc]initWithDictionary:[sub objectAtIndex:0]];
NSLog([it objectForKey:@"title"]);
}
这是我的代码。但我不明白为什么我不能 NSLog [it objectForKey:@"title"]。
I've this code
SBJsonParser *vol= [[SBJsonParser alloc]init];
if (body) {
NSArray *feeds = [vol objectWithString:body error:nil];
NSDictionary *results = [body JSONValue];
NSArray *subs = [results valueForKey:@"subscriptions"];
NSArray *list;
NSMutableArray *sub;
for (NSDictionary *iscrizione in subs){
NSLog([iscrizione objectForKey:@"title"]);
NSLog([iscrizione objectForKey:@"htmlUrl"]);
list=[NSArray arrayWithObjects:[iscrizione objectForKey:@"title"], [iscrizione objectForKey:@"htmlUrl"], nil];
[sub addObject:list];
}
But when I try to add list to the NSMutableArray the program crashes. Why?
How can I insert [iscrizione objectForKey:@"title"]
and [iscrizione objectForKey:@"htmlUrl"]
in a data structure?
EDIT**
SBJsonParser *vol= [[SBJsonParser alloc]init];
if (body) {
NSArray *feeds = [vol objectWithString:body error:nil];
NSDictionary *results = [body JSONValue];
NSArray *subs = [results valueForKey:@"subscriptions"];
NSMutableArray *sub = [[NSMutableArray alloc] initWithCapacity:[subs count]];
for (NSDictionary *iscrizione in subs){
[sub addObject:iscrizione];
}
NSDictionary *it=[[NSDictionary alloc]initWithDictionary:[sub objectAtIndex:0]];
NSLog([it objectForKey:@"title"]);
}
This is my code. But I can't understand why I cannot NSLog [it objectForKey:@"title"].
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有初始化 NSMutableArray。
应该是
NSMutableArray *sub = [[NSMutableArray alloc] initWithCapacity:[subs count]];
从技术上讲,您可以简单地分配它
NSMutableArray *sub = [[NSMutableArray 数组];
也是如此,但这可能会更有效,因为您已经知道会有多少数据条目。
只要注意内存管理,在第一种情况下需要释放它,在第二种情况下它会自动释放。
You didn't initialize your NSMutableArray.
Should be
NSMutableArray *sub = [[NSMutableArray alloc] initWithCapacity:[subs count]];
Technically, you could simply allocate it with
NSMutableArray *sub = [[NSMutableArray array];
as well but this could be more efficient, as you already know how many data entries there will be.
Just pay attention to memory management, you need to release it in the first case and it's autoreleased in the second case.
sub
未分配内存。您可以在 if 条件之外创建一个空数组NSMutableArray *sub = [NSMutableArray array];
或在代码中维护引用。sub
is not allocated memory. You can create a empty arrayNSMutableArray *sub = [NSMutableArray array];
outside the if condition or have a reference maintained in your code.以下将正常工作...
Following will do work properly...
在使用 MutableArray 之前,您应该
alloc
并init
它。You should
alloc
andinit
the MutableArray before you can use it.