相关疑难解决方法(0)

在实现API时,如何避免在块中捕获self?

我有一个工作的应用程序,我正在努力将其转换为Xcode 4.2中的ARC.其中一个预检警告涉及self强烈捕获导致保留周期的块.我已经制作了一个简单的代码示例来说明问题.我相信我理解这意味着什么,但我不确定实现这种情况的"正确"或推荐方法.

  • self是MyAPI类的一个实例
  • 下面的代码被简化为仅显示与我的问题相关的对象和块的交互
  • 假设MyAPI从远程源获取数据,MyDataProcessor处理该数据并生成输出
  • 处理器配置有块以通信进度和状态

代码示例:

// code sample
self.delegate = aDelegate;

self.dataProcessor = [[MyDataProcessor alloc] init];

self.dataProcessor.progress = ^(CGFloat percentComplete) {
    [self.delegate myAPI:self isProcessingWithProgress:percentComplete];
};

self.dataProcessor.completion = ^{
    [self.delegate myAPIDidFinish:self];
    self.dataProcessor = nil;
};

// start the processor - processing happens asynchronously and the processor is released in the completion block
[self.dataProcessor startProcessing];
Run Code Online (Sandbox Code Playgroud)

问题:我在做什么"错误"和/或如何修改它以符合ARC惯例?

objective-c ios objective-c-blocks automatic-ref-counting

222
推荐指数
6
解决办法
8万
查看次数

performSelector:withObject:afterDelay:不进行调用

在一个方法中,我想在n秒后调用一个方法:

    self.toolBarState = [NSNumber numberWithInt:1];
    [self changeButtonNames];
    [self drawMap];
    [self performSelector:@selector(showActionSheet) withObject:nil afterDelay:2];
Run Code Online (Sandbox Code Playgroud)

我想在drawMap完成后2秒显示操作表.当我使用这个performSelector时,它从不拨打电话.

如果我只是把[self showActionSheet];,它的工作正常.有没有理由说performSelector没有打电话?

编辑:在我的代码的另一部分,我进行相同的调用,它的工作原理:

HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.delegate = (id) self;
[HUD showWhileExecuting:@selector(drawMap) onTarget:self withObject:nil animated:YES];

[self performSelector:@selector(showActionSheet) withObject:nil afterDelay:6];
Run Code Online (Sandbox Code Playgroud)

这里,showActionSheet在drawMap完成后6秒被调用.我猜我的线程还有什么东西我还不明白......

EDIT2:

-(void)showActionSheet
{
    InspectAppDelegate *dataCenter = (InspectAppDelegate *) [[UIApplication sharedApplication] delegate];

    if (dataCenter.fieldIDToPass == nil)
    {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Selected Boundary Options" delegate:(id) self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Analyze a Field",@"Retrieve Saved Analysi", @"Geotag Photos", @"Refresh the map",nil];
        actionSheet.tag = 0;
        [actionSheet …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c ios

17
推荐指数
2
解决办法
1万
查看次数