简体   繁体   中英

iPhone Objective-C: Exact same method call, working the first time, but getting “unrecognized selector” the second

I am working with a UITableViewController. The method that fails is:

- (void) testLoc {
    self.dictionary = [self.delegate getLocationsWithLatitude:@"0.0" longitude:@"0.0"]; //HERE
    [self setUpTableArray:dictionary]; 
}

Test results have shown that the exception is thrown in the first line of testLoc .

- (NSDictionary *) getLocationsWithLatitude:(NSString *)latitude longitude:(NSString *)longitude;

All I do in the above method is make an HTTP request and return an NSDictionary.

Below is my viewDidLoad (the first method call, which works) for the UITableViewController:

- (void)viewDidLoad {
    self.delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [self testLoc];
    [super viewDidLoad];
}

And here is my viewWillAppear, which I get "unrecognized selector" from:

- (void)viewWillAppear:(BOOL)animated {
    self.delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [self testLoc];
    [super viewWillAppear:animated];
}

Here is the error that I am getting:

-[NSCFString key]: unrecognized selector sent to instance 0x141770

The reason I am doing this is because I have a table view that I want to update every time I tab back to it with a tab bar.

This is for the first real app that I am building, and I would really appreciate any kind of insight. Thanks!!

UPDATE

Here is getLocationsWithLatitude:

- (NSDictionary *) getLocationsWithLatitude:(NSString *)latitude longitude:(NSString *)longitude {
OAMutableURLRequest *locationsRequest = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://somerequesturl"] 
                                                                        consumer:self.globals.consumer 
                                                                           token:self.globals.requestToken 
                                                                           realm:nil signatureProvider:nil];
[locationsRequest setHTTPMethod:@"GET"];

[locationsRequest setParameters:[NSArray arrayWithObjects:
                                 [OARequestParameter requestParameter:@"geolat" value:latitude],
                                 [OARequestParameter requestParameter:@"geolong" value:longitude],
                                 nil]];

[locationsRequest prepare];
NSData *locationsData = [NSURLConnection sendSynchronousRequest:locationsRequest returningResponse:nil error:nil];
NSString *locationsString = [[[NSString alloc] initWithData:locationsData encoding:NSUTF8StringEncoding] autorelease];

    [locationsRequest release];
    SBJSON *jsonParser = [SBJSON new];
    NSError *error = nil;
    return [jsonParser objectWithString:locationsString error:&error]; 
}

Here is setUpTableArray:

- (void) setUpTableArray:(NSDictionary *)dict {

    NSArray *array = [dict objectForKey:@"groups"];
    if (array != nil) {
        self.placesArray = array;
    }
}

What happens if you change viewWillAppear like this:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self testLoc];
}

Wow, I definitely found my own problem this time. Just get rid of [locationsRequest prepare]; I definitely saw code somewhere that has that, but this tells me otherwise.

EDIT Turns out this was a memory-memory allocation error. Getting rid of that line wasn't the actual fix. It seems to be some problem with a string being auto-released. I asked a follow up question here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM