Return Result of Completion Block

后端 未结 2 662
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 03:32

So I\'m trying to build a layer on top of the Twitter API (among others) for a project and I need to find a way to return the result of the Twitter actions to the layer of a

2条回答
  •  情书的邮戳
    2020-12-07 04:02

    Why don't you also use a block to return the response? Something like:

    -(void)sendTweet:(Tweet *)tweet withResponseCallback:(void (^)(NSMutableDictionary *responseDictionary))callback {
    
        NSLog(@"Sending tweet");
    
        NSMutableDictionary *twitterRequestDictionary = [[NSMutableDictionary alloc] init];
        [twitterRequestDictionary setObject:tweet.tweetBody forKey:@"status"];
    
        TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.twitter.com/1/statuses/update.json"]
                                                 parameters:twitterRequestDictionary
                                              requestMethod:TWRequestMethodPOST];
        [request setAccount:self.userAccount];
    
    
        [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
            NSMutableDictionary *responseDictionary  = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"Response dictionary: %@", responseDictionary);
            callback(responseDictionary);
        }];
    }
    

提交回复
热议问题