在块内访问实例变量 - Objective-C

时间:2014-04-14 08:05:27

标签: objective-c objective-c-blocks

我有我的实例变量声明:

@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。但是,我收到错误:

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;
}

3 个答案:

答案 0 :(得分:2)

在阻止之前,请使用以下代码行修改self

// Weak self
__weak MyViewController *weakSelf = self;

并像这样调用你的变量:

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) {
                [weakSelf.avatars setObject:image forKey:key];
                NSLog(@"%@", weakSelf.avatars);
                imageBlock( image );
            } else {
                errorBlock();
            }
        });
    });
}

您可以参考here获取有关block的强大答案!

答案 1 :(得分:1)

您的代码不是方法,而是简单的C函数。因此,没有" self"。类方法以" +"开头,实例方法以" - "开头。

答案 2 :(得分:1)

您可以使UIImageFromURL()成为实例方法而不是普通的C函数, 然后它可以访问self

- (void)fetchImageFromURL:(NSURL *)URL key:(NSString *)key imageBlock:(void(^)(UIImage *image))imageBlock errorBlock:(void (^)(void))errorBlock
{
    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();
            }
        });
    });
}

然后将其称为

[self fetchImageFromURL:imageURL key:avatarURL imageBlock:^(UIImage *image) {
    [cell.avatarImage setImage:image];
} errorBlock:^{
    NSLog(@"Failed to fetch profile picture.");
}];