我的应用程序转到视图控制器,发出两个自动服务器请求,建立连接,检索数据并正确显示它,然后完成。用户单击“喜欢”按钮,又发出了两个服务器请求 - 成功。显示正确。需要被完成。然后它崩溃,并出现错误:
[__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 活动发生一样。不应该有。
以前有人见过这种东西吗?非常感谢您的帮助!