[__NSCFNumber length]:无法识别的选择器发送到实例0x6d21350

Ric*_*nop 20 iphone xcode ios

这个错误意味着什么?

[__NSCFNumber length]: unrecognized selector sent to instance 0x6d21350
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

    NSString *urlString = @"http://api.twitter.com/1/statuses/update.json";
    NSURL *url = [NSURL URLWithString:urlString];

    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:status forKey:@"status"];
    [params setObject:replyToID forKey:@"in_reply_to_status_id"];
    [params setObject:@"1" forKey:@"include_entities"];

    // Build the request with our parameter
    TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodPOST];

    // Attach the account object to this request
    [request setAccount:twitterAccount];

    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        if (!responseData) {
            // inspect the contents of error 
            NSLog(@"%@", [error localizedDescription]);

            self.alert = [[UIAlertView alloc] initWithTitle:@"HTTP error" message:@"I could not connect to the Twitter API." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [self.alert show];

            [self.replyDelegate replyRequestSuccessful:NO];
        }
        else {
            /*NSString *responseDataAsString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
             NSLog(responseDataAsString);*/

            NSError *error;
            NSArray *replyResponse = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];

            if (!replyResponse) {
                NSLog(@"%@", [error localizedDescription]);

                self.alert = [[UIAlertView alloc] initWithTitle:@"JSON error" message:@"I could not parse the JSON response from the Twitter API." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
                [self.alert show];

                [self.replyDelegate replyRequestSuccessful:NO];
            }
            else {
                [self.replyDelegate replyRequestSuccessful:YES];
            }
        }
    }];
Run Code Online (Sandbox Code Playgroud)

我尝试了debuggin,一旦进入performRequestWithHandler就会死掉.它会进入else块并因上述错误而死亡.

zou*_*oul 69

这意味着您正在传递NSNumber被调用代码所需的位置NSString或具有length方法的其他对象.您可以告诉Xcode中断异常,以便您可以看到length调用该方法的确切位置.

  • 更可能的是,他的内存管理存在问题,并且运行时/框架期望NSString不再存在(来自过早释放),并且已经在那里分配了NSNumber.如果你故意尝试将`length`发送到`NSNumber`,编译器会希望警告你. (8认同)
  • 如果一个接口使用`id`而不是`NSString`,很容易将`NSNumber`放在需要`NSString`的地方,而不需要编译器注意到.一般来说,"类型擦除"正在剥离显式类型.这是集合在某些语言中的常见问题:当你将某些东西放入Objective-C集合(如`NSArray`)然后再读回来时,编译器不再知道事物的类型,它已被删除. (4认同)

小智 6

特别是这行代码:

[params setObject:replyToID forKey:@"in_reply_to_status_id"];
Run Code Online (Sandbox Code Playgroud)

replyToID是不是一个字符串或有一个方法length.如果您没有这个,或者replyToID在将它设置为params之前将其转换为String,它应该可以工作.