视图控制器URL请求和连接正确完成后崩溃

时间:2012-12-05 15:44:42

标签: ios xcode connection request unrecognized-selector

我的应用程序转到viewcontroller,发出两个自动服务器请求,建立连接,检索数据并正确显示它,并完成。用户单击“喜欢”按钮,并成功完成另外两个服务器请求。显示是正确的。需要被完成。然后它崩溃,错误:

[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance

我正在使用非常方便的SimplePost类(由Nicolas Goles提供)。以下是我的请求,它们都在viewDidLoad中调用:

- (void) setScore {
    Profile *newPf = [[Profile alloc] initID:thisUser profil:@"na" scor:score];
    NSMutableURLRequest *reqPost = [SimplePost urlencodedRequestWithURL:[NSURL URLWithString:kMyProfileURL] andDataDictionary:[newPf toDictPf]];
    (void) [[NSURLConnection alloc] initWithRequest:reqPost delegate:self];
}
- (void) saveHist {
    History *newH = [[History alloc] initHistID:thisUser hQid:thisQstn hPts:score hLiked:NO];
    NSMutableURLRequest *reqHpost = [SimplePost urlencodedRequestWithURL:[NSURL URLWithString:kMyHistURL] andDataDictionary:[newH toDictH]];
    (void) [[NSURLConnection alloc] initWithRequest:reqHpost delegate:self];
}

我的自定义类(配置文件和历史记录)中唯一的“新”事物是hLiked的BOOL,但它“正常” - 数据库正在正确更新。
然后,用户可以单击“喜欢”按钮(+或 - )。以下是其他要求:

- (IBAction)likeClick:(id)sender {
    double stepperValue = _likeStepper.value;
    _likesLbl.text = [NSString stringWithFormat:@"%.f", stepperValue];
    [self updateLikes];
    [self updateHist];
}
- (void) updateLikes {
    //  update the question with the new "liked" score
    NSInteger likesN = [_likesLbl.text integerValue];
    Questn *qInfo = [[Questn alloc] initQwID:thisQstn askID:0 wCat:@"na" wSit:@"na" wAns1:@"na" wPts1:0 wAns2:@"na" wPts2:0 wAns3:@"na" wPts3:0 wAns4:@"na" wPts4:0 wJust:@"na" wLikes:likesN ];
    NSMutableURLRequest *reqPost = [SimplePost urlencodedRequestWithURL:[NSURL URLWithString:kLikesURL] andDataDictionary:[qInfo toDictQ]];
    (void) [[NSURLConnection alloc] initWithRequest:reqPost delegate:self];
}
- (void) updateHist {
    History *newH = [[History alloc] initHistID:thisUser hQid:thisQstn hPts:98989 hLiked:YES];
    NSMutableURLRequest *reqHpost = [SimplePost urlencodedRequestWithURL:[NSURL URLWithString:kHistURL] andDataDictionary:[newH toDictH]];
    (void) [[NSURLConnection alloc] initWithRequest:reqHpost delegate:self];
}

凌乱,对吧?这是我的连接代码:

//  connection to URL finished with Plist-formatted user data array returned from PHP
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSDictionary *array = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:0 errorDescription:nil];
    BOOL keyLikeExists = [array objectForKey:@"likes"] != nil;
    if( keyLikeExists ) {
        _likesLbl.text = [array objectForKey:@"likes"];
    }
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Connection did fail." );
}

这一切都做得很好,然后几秒钟后它就崩溃了上面提到的“无法识别的选择器”错误,就像还有一些URL活动正在发生。不应该有。

之前有人见过这种事吗?非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

在代码的某处,可以调用方法isEqualToString:。发送该消息的是NSNumber对象而不是字符串。要么存在关于对象类型的逻辑问题,要么存在一个内存问题,即字符串被过度释放并且其内存被重新用于保存数字。

如果没有看到通话的背景,就很难猜到。

如果中断异常,堆栈跟踪应告诉您代码中的哪个位置失败。