相关疑难解决方法(0)

通过弱指针分配到块中的ivar

isFinished我的界面文件中有一个只读属性:

typedef void (^MyFinishedBlock)(BOOL success, NSError *e);

@interface TMSyncBase : NSObject {
     BOOL isFinished_;
}

@property (nonatomic, readonly) BOOL isFinished;
Run Code Online (Sandbox Code Playgroud)

我想YES稍后在某个块中将它设置为一个块,而不创建一个保留周期self:

- (void)doSomethingWithFinishedBlock:(MyFinishedBlock)theFinishedBlock {
    __weak MyClass *weakSelf = self;
    MyFinishedBlock finishedBlockWrapper = ^(BOOL success, NSError *e) {
        [weakSelf willChangeValueForKey:@"isFinished"];
        weakSelf -> isFinished_ = YES;
        [weakSelf didChangeValueForKey:@"isFinished"];
        theFinishedBlock(success, e);
    };

    self.finishedBlock = finishedBlockWrapper; // finishedBlock is a class ext. property
}
Run Code Online (Sandbox Code Playgroud)

我不确定这是正确的做法.这段代码会泄漏,破坏,还是没问题?也许有一种我更容易被忽视的方式?

objective-c objective-c-blocks ivar automatic-ref-counting declared-property

7
推荐指数
1
解决办法
3731
查看次数

更改块中的实例变量

我对如何更改块内的实例变量感到很困惑.

接口文件(.h):

@interface TPFavoritesViewController : UIViewController {
    bool refreshing;
}
Run Code Online (Sandbox Code Playgroud)

实施:

__weak TPFavoritesViewController *temp_self = self;
refreshing = NO;
[myTableView addPullToRefreshWithActionHandler:^{
    refreshing = YES;
    [temp_self refresh];
}];
Run Code Online (Sandbox Code Playgroud)

正如您可能猜到的那样,当我尝试更改块内的刷新ivar时,我会收到保留周期警告.如果没有出错,我该怎么做?

xcode warnings block objective-c ivar

7
推荐指数
1
解决办法
2312
查看次数