我有一个基于块的枚举设置来遍历一个 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);
contentURL
如果我使用这种方法,当我尝试在 NSLog 语句中打印出来时,会出现 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);
一切正常。为什么是这样?