EXC_BAD_ACCESS when using stringWithFormat?

ios, xcode

Solution

You must not format `NSInteger` (which is just a typedef'd `int` on current iOS versions) with the `%@` specifier. Writing `%@` in a string format basically means "call `description` on the object and use the result". But NSInteger is not an object, it's a primitive type. You get a memory exception because when listID is 42 you access an object at memory address 42. This is definitely not what you want.

-(NSDictionary *)syncWithList:(NSInteger)listID
                               ^^^^^^^^^
NSString *urlit = [NSString stringWithFormat:@"http://0.0.0.0:3000/lists/%@/syncList.json?auth_token=%@",@"xxxxxxxxxxx",listID];
                                                                                                     ^^

just use the `%i` format specifier instead of `%@` for listID.

NSString *urlit = [NSString stringWithFormat:@"http://0.0.0.0:3000/lists/%@/syncList.json?auth_token=%i",@"xxxxxxxxxxx",listID];

Problem

While deploying my application, I got the error message: "Thread 1:Program received signal: "EXC_BAD_ACCESS". My code is below: ``` -(NSDictionary *)syncWithList:(NSInteger)listID { NSString *urlit = [NSString stringWithFormat:@"http://0.0.0.0:3000/lists/%@/syncList.json?auth_token=%@",@"xxxxxxxxxxx",listID]; // **Here I got the error message: "Thread 1:Program received signal: "EXC_BAD_ACCESS"** NSLog(@"url: %@",urlit); NSURL *freequestionurl = [NSURL URLWithString:urlit]; ASIHTTPRequest *back = [ASIHTTPRequest requestWithURL:freequestionurl]; [back startSynchronous]; self.listData = [[back responseString] objectFromJSONString]; NSLog(@"%@",listData); NSDictionary *dicPost = [listData objectAtIndex:0]; return dicPost; } ``` Thanks a lot!!!!

Original source