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



我的应用程序进入一个视图控制器,发出两个自动服务器请求,建立连接,检索数据并正确显示,然后就完成了。用户点击一个"点赞"按钮,然后又发出了两个服务器请求——成功。显示正确。应该这样做。然后它崩溃了,并出现错误:

[__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];
}

我的自定义类(Profile和History)唯一的"新"东西是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活动在发生一样。不应该有。

以前有人见过这种东西吗?非常感谢您的帮助!

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

如果看不到通话的背景,很难猜测。

如果您中断了异常,堆栈跟踪应该会告诉您它在代码中的何处失败。

最新更新