我有我的实例变量声明:访问实例变量 - Objective-C的
@interface TimelineTableViewController()
@property (strong, nonatomic) NSMutableArray *avatars;
@end
然后我的块:
void UIImageFromURL(NSURL *URL, NSString *key, void(^imageBlock)(UIImage *image), void(^errorBlock)(void)) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
NSData *data = [[NSData alloc] initWithContentsOfURL:URL];
UIImage *image = [[UIImage alloc] initWithData:data];
dispatch_async(dispatch_get_main_queue(), ^(void){
if (image != nil) {
[self.avatars setObject:image forKey:key];
NSLog(@"%@", self.avatars);
imageBlock(image);
} else {
errorBlock();
}
});
});
}
我想要做的,就是高速缓存下载映像到实例数组以避免重新下载每个TableCell get的重新创建。但是,我得到的错误:
Use of undeclared identifier 'self'
我打电话给我块像这样,如果这能帮助:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TweetCell *cell = (TweetCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *tweetObject = _tweets[indexPath.row];
NSDictionary *userObject = tweetObject[@"user"];
NSString *avatarURL = userObject[@"profile_image_url"];
avatarURL = [avatarURL stringByReplacingOccurrencesOfString:@"_normal"
withString:@""];
NSURL *imageURL = [NSURL URLWithString:avatarURL];
UIImageFromURL(imageURL, avatarURL, ^(UIImage *image) {
[cell.avatarImage setImage:image];
}, ^{
NSLog(@"Failed to fetch profile picture.");
});
return cell;
}
嗯,我用这:'__weak TimelineTableViewController * weakSelf = self;'仍然可以使用未声明的标识符self'。我使用了'TimelineTableViewController',因为我的类是:'@interface TimelineTableViewController()',这是正确的吗? – benhowdle89