次のような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);
すべて正常に動作します。どうしてこれなの?