- (無效)fetchLastMessageInChannel
{ __weak ID weakSelf =自我;如何從完成塊中獲取變量?
for (ANKChannel *channel in self.channelArray)
{
NSLog(@"channels %@",channel);
NSLog(@"channel last message %@",channel.latestMessageID);
[[ClientManager currentClient] fetchMessageWithID:channel.latestMessageID inChannel:channel
completion:^(id responseObject, ANKAPIResponseMeta *meta, NSError *error)
{
NSLog(@"message object %@",responseObject);
ANKMessage *message = responseObject;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf populateTextViews:message.text];
});
NSLog(@"message text %@",message.text);
}];
}
}
- (空)populateTextViews:(的NSString *)消息 {
NSMutableArray *textViews = [@[] mutableCopy];
NSMutableAttributedString *postText = [[NSMutableAttributedString alloc] initWithString:message];
[postText addAttributes:@{
NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleBody],
NSForegroundColorAttributeName : [UIColor darkTextColor]
}
range:NSMakeRange(0, postText.length)];
UITextView *postTextView = [[UITextView alloc] initWithFrame:CGRectMake(80, 30, kPostLabelMaxWidth, 44)];
postTextView.attributedText = postText;
postTextView.dataDetectorTypes = UIDataDetectorTypeAll;
postTextView.backgroundColor = [UIColor whiteColor];
postTextView.editable = NO;
postTextView.scrollEnabled = NO;
postTextView.clipsToBounds = NO; // So it doesn't clip the text selector
CGRect textViewBounds = postTextView.bounds;
textViewBounds.origin = CGPointMake(80, 30);
textViewBounds.size.width = MAX(textViewBounds.size.width, kPostLabelMaxWidth);
textViewBounds.size.height = postTextView.contentSize.height;
postTextView.bounds = textViewBounds;
[postTextView sizeToFit]; // Reload the content size
[textViews addObject:postTextView];
self.channelTextViewArray = [textViews copy];
}
這是我目前的處境至於我的方法去我收到的幫助。 self.channelTextViewArray返回nil並導致崩潰,因爲populateTextViews(NSString *)消息永遠不會被調用。
任何想法?
,因爲有一個「小」的問題與你的邏輯:在信息,你想要得到_outside_塊的範圍將可用_after_了'NSLog'運行_outside_的塊。你只需要同步這些線程來達到你想要的效果,但是你不應該阻塞主線程直到塊完成。可能,你需要爲它使用不同的代碼模式,而不是你已經使用的代碼模式。 – holex
我是一個自學成才的程序員,只有8個月的經驗,所以我不知道任何事情,但我很感激幫助。你所談論的代碼模式的任何建議? –
通過'GitHub'或'Bitbucket'向我發送源代碼鏈接,我會看看它。 – holex