【发布时间】:2013-07-22 21:00:10
【问题描述】:
我有一个基于块的枚举设置来遍历一个 NSDictionaries 数组,如下所示:
__block NSURL *contentURL;
//This method of enumerating over the array gives the bad_access error
[documents enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *aName = [(NSDictionary *)obj objectForKey:@"Name"];
if([aName isEqualToString:name]) {
contentURL = [NSURL URLWithString:[(NSDictionary *)obj objectForKey:@"Content"]];
*stop=YES;
}
}];
NSLog(@"Content URL for issue with name %@ is %@", name, contentURL);
如果我使用这种方法,当我尝试在 NSLog 语句中打印出来时,我会在contentURL 上收到 EXC_BAD_ACCESS 错误。
但是,如果我这样枚举数组:
NSURL *contentURL;
//This method of enumerating over the array works fine
for (NSDictionary *obj in documents) {
NSString *aName = [obj objectForKey:@"Name"];
if([aName isEqualToString:name]) {
contentURL = [NSURL URLWithString:[obj objectForKey:@"Content"]];
}
}
NSLog(@"Content URL for issue with name %@ is %@", name, contentURL);
一切正常。为什么是这样?
【问题讨论】:
-
I get a EXC_BAD_ACCESS error on contentURL.这是否意味着调试器停止分配? -
对不起,不在作业中,当我尝试在 NSLog 中打印时
-
使用您的代码,我只能怀疑变量
name是问题所在。我可以看到它的定义以及你在哪里给它赋值吗?另外,您可能正在使用手动引用计数吗?
标签: objective-c nsarray objective-c-blocks enumeration