尝试使用TWRequest发布时,将“无法识别的选择器发送到实例”

时间:2013-03-20 15:27:31

标签: ios twitter twrequest acaccount

我正在尝试使用TWRequest对象发布图像。 这是我的代码:

ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    NSData *pngImage = UIImagePNGRepresentation(image);
    NSData *tweetText = [userText copy];
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
        if (granted) {
            NSLog(@"Twitter is good to go");
            NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];
            if (twitterAccounts.count) {
                ACAccount *twitterAccount = [twitterAccounts objectAtIndex:0];
                NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update_with_media.json"];
                TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:nil requestMethod:TWRequestMethodPOST];
                [request addMultiPartData:pngImage withName:@"media" type:@"image/png"];
                [request addMultiPartData:tweetText withName:@"text" type:@"text/plain"];
                [request setAccount:twitterAccount];
                [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                    if (responseData)
                    {
                        NSError *error = nil;
                        NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
                        if (jsonArray) {
                            for(NSDictionary *item in jsonArray) {
                                NSLog(@"Item: %@", item);
                            }
                        }
                        else
                        {
                            NSLog(@"Error %@ with user info %@.", error, error.userInfo);
                        }
                    }
                }];                                   
            }
        } else {
            NSLog(@"The user does not grant us permission to access its Twitter account(s).");
        }
    }];

每当程序到达[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {行时,我都会收到以下错误:

  

[__ NSCFString bytes]:无法识别的选择器发送到实例0x1d8f4250    *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSCFString bytes]:无法识别的选择器发送到实例0x1d8f4250'   * 第一次抛出调用堆栈:   (0x33ec02a3 0x3bb5a97f 0x33ec3e07 0x33ec2531 0x33e19f68 0x3473b90b 0x35c416fd 0x35c417a3 0x35c4065f 0x35c3fa6b 0x35c40ab9 0x5adc5 0x3bf7211f 0x3bf7fdcb 0x3bf80259 0x3bf803b9 0x3bfa6a11 0x3bfa68a4)   libc ++ abi.dylib:terminate调用抛出异常   (lldb)

1 个答案:

答案 0 :(得分:6)

问题在于tweetText。您正在复制NSString,但将其分配给NSData对象。

改变这个:

NSData *tweetText = [userText copy];

到此:

NSData *tweetText = [userText dataUsingEncoding:NSUTF8StringEncoding];