我想用__block变量块中获取价值。 但是,当出块之后,__block变量似乎是零。 为什么会发生这种情况?
NSString *fileName = [Tools MD5Encode:url];
__block NSString *filePath = nil;
[fileList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *aFileName = obj;
if ([aFileName isEqualToString:fileName]) {
NSString *path = [VERSIONS_INFO_DATA_DIRECTORY stringByAppendingPathComponent:aFileName];
filePath = path;
NSLog(@"filePath1 %@", filePath);
*stop = YES;
}
}];
//NSLog(@"filePath2 %@", filePath);
//filePath seems to be nil
return filePath;
当我修改代码,[路径复制],它的工作原理。 但我不知道这是否是一个好主意。 任何决定?
NSString *fileName = [Tools MD5Encode:url];
__block NSString *filePath = nil;
[fileList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *aFileName = obj;
if ([aFileName isEqualToString:fileName]) {
NSString *path = [VERSIONS_INFO_DATA_DIRECTORY stringByAppendingPathComponent:aFileName];
filePath = [path copy];
NSLog(@"filePath1 %@", filePath);
*stop = YES;
}
}];
//NSLog(@"filePath2 %@", filePath);
return [filePath autorelease];