简体   繁体   中英

EXC_BAD_ACCESS when using stringWithFormat?

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!!!!

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

EDIT: So used to getting errors from Xcode without it giving me any clues I neglected to notice that the troubled line was already know. I'll leave this here in the hope it helps someone in future.

Try creating an exception breakpoint, it may point to straight to the line where your code is falling over which should help you figure out the problem.

  1. Switch to the breakpoint 'tab' in the left hand navigator.
  2. Click the little '+' at the bottom.
  3. Create a breakpoint as shown in the image:
  4. Run your code and see where it pops.

添加断点

You used wrong data type to print out.

NSLog(@"%@",listData);

You made very popularar mistake in this line

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

Second argument is of type NSInteger, but in format you use %@ , this is object only, and compiler thinks that your listID is address of object. Correct format is %li :

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

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