Crashing after view controller URL request and connection finish correctly

江枫思渺然 提交于 2019-12-12 00:33:58

问题


My app goes to a viewcontroller, makes two automatic server requests, makes the connection, retrieves the data and correctly displays it, and is done. The user clicks a "likes" button and two more server requests are made - successfully. Displays are correct. Should be done. Then it crashes, with the error:

[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance

I'm using the very handy SimplePost class (by Nicolas Goles). Here are my requests, which are both called in 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];
}

The only "new" thing with my custom classes (Profile and History) is the BOOL for hLiked, but it's "working" - the database is updating correctly.
Then, the user can click a "Likes" button (+ or -). Here are the other requests:

- (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];
}

Messy, right? Here's my connection code:

//  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." );
}

It all does a good job, and then a couple of seconds later it crashes with that "unrecognized selector" error mentioned above, like there's still some URL activity happening. There shouldn't be.

Anybody seen this kind of thing before? Many thanks for any help!


回答1:


Somewhere in your code there's a call to the method isEqualToString:. The thing that's being sent that message is a NSNumber object rather than a string. Either there's a logic problem concerning the object type or there's a memory problem where a string was over-released and its memory is being re-used to hold a number.

Without seeing the context for the call, it's hard to guess.

If you break on the exception, the stack trace should tell you where in the code it's failing.



来源:https://stackoverflow.com/questions/13726976/crashing-after-view-controller-url-request-and-connection-finish-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!