我刚刚开始学编程和Objective-C,试图找出代码的问题所在。我的代码使用了iOS 5 Twitter框架。我使用了苹果提供的大部分示例代码,所以一开始我并不知道我在使用一个块作为完成处理程序。
现在我收到来自Xcode 4的两条消息,分别是“1. 块将被强烈保留由捕获对象强烈保留”和“在此块中强烈捕获“self”可能会导致保留周期”。
基本上,我所做的就是删除苹果在他们的完成处理程序中使用的代码(switch语句与TWTweetComposeViewControllerResultCancelled&TWTweetComposeViewControllerResultDone),并使用我的if语句与
因此,在推文中添加图像后调用
我希望有人能解释一下为什么会发生这种情况以及我如何解决它。此外:我可以将完成处理程序代码放入方法中而不是块中吗?
现在我收到来自Xcode 4的两条消息,分别是“1. 块将被强烈保留由捕获对象强烈保留”和“在此块中强烈捕获“self”可能会导致保留周期”。
基本上,我所做的就是删除苹果在他们的完成处理程序中使用的代码(switch语句与TWTweetComposeViewControllerResultCancelled&TWTweetComposeViewControllerResultDone),并使用我的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];
}
}