iOS 5 Twitter Framework & completionHandler block - “Capturing 'self' strongly in this block is likely to lead to a retain cycle”

前端 未结 4 1115
失恋的感觉
失恋的感觉 2021-02-14 13:51

I am very new to programming and Objective-C and I am trying to work out what is wrong with my code. I have read a bit about blocks but I don\'t know how any of what I have read

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-14 14:18

    Your block is retaining self because you're using self as the delegate of the UIAlertView. If self is also retaining the block, they're retaining each other, which creates a retain cycle.

    Try

     __block WhateverTypeSelfIs *nonRetainedSelfForBlock = self;
    [tweetViewController setCompletionHandler: 
    

    and

    UIAlertView *alert = [[UIAlertView alloc] 
                                     initWithTitle:alertTitle 
                                     message:alertMessage 
                                     delegate:nonRetainedSelfForBlock 
                                     cancelButtonTitle:alertCancelButtonTitle 
                                     otherButtonTitles:otherAlertButtonTitle,nil];
    

    Check the Apple docs, section Object and Block Variables. Objects referenced within a block are retained, unless you use __block.

提交回复
热议问题