【发布时间】:2011-12-30 20:25:05
【问题描述】:
我对编程和 Objective-C 非常陌生,我正在尝试找出我的代码有什么问题。我已经阅读了一些关于块的内容,但我不知道到目前为止我所阅读的内容与我的代码有什么关系。
我的代码使用的是 iOS 5 Twitter 框架。我使用了 Apple 提供的大部分示例代码,所以一开始我实际上并不知道我正在使用块作为完成处理程序。
现在我从 Xcode 4 收到这两条消息,说“1. 块将被捕获的对象强烈保留的对象保留”和“在此块中强烈捕获'self'可能会导致一个保留周期”。
基本上,我所做的是删除 Apple 在其完成处理程序中使用的代码(使用 TWTweetComposeViewControllerResultCancelled 和 TWTweetComposeViewControllerResultDone 的 switch 语句)并将我的 if 语句与 [imagePickerController sourceType] 一起使用。
所以sendTweet 在图像被添加到推文后被调用。
我希望有人可以向我解释为什么会发生这种情况以及我如何解决它。另外:我可以将完成处理程序代码放入方法而不是块中吗?
- (void)sendTweet:(UIImage *)image
{
//adds photo to tweet
[tweetViewController addImage:image];
// Create the completion handler block.
//Xcode: "1. Block will be retained by an object strongly retained by the captured object"
[tweetViewController setCompletionHandler:
^(TWTweetComposeViewControllerResult result) {
NSString *alertTitle,
*alertMessage,
*otherAlertButtonTitle,
*alertCancelButtonTitle;
if (result == TWTweetComposeViewControllerResultCancelled)
{
//Xcode: "Capturing 'self' strongly in this block is likely to lead to a retain cycle"
if ([imagePickerController sourceType])
{
alertTitle = NSLocalizedString(@"TCA_TITLE", nil);
alertMessage = NSLocalizedString(@"TCA_MESSAGE", nil);
alertCancelButtonTitle = NSLocalizedString(@"NO", nil);
otherAlertButtonTitle = NSLocalizedString(@"YES", nil);
//user taps YES
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:alertTitle
message:alertMessage
delegate:self // Note: self
cancelButtonTitle:alertCancelButtonTitle
otherButtonTitles:otherAlertButtonTitle,nil];
alert.tag = 1;
[alert show];
}
}
【问题讨论】:
标签: iphone objective-c ipad ios5 objective-c-blocks