jxdwinter
jxdwinter

Reputation: 2369

String with error:EXC_BAD_ACCESS

AppDelegate has a parameter called user,

and user has two parameters called userId & departmentId.

I just want to access my WebService use the ASIHttpRequest API,

but before I send the request,

there is error EXC_BAD_ACCESS with in this code:

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    NSString *userId = appDelegate.user.userId;
    NSString *departmentId = appDelegate.user.departmentId;
    NSLog(@"xxxxx:%@",userId);//this can log 29
    NSLog(@"xxxxx:%@",departmentId);//this can log 17
    NSString *URL = [NSString stringWithFormat:@"https://xxx.xxx.xx.xx/FMS/Pages/Service/FMService.svc/GetAnnouncement?userId=%@&departmentId=%@&pageIndex=%@&pageSize=%@",userId,departmentId,1,10]; ***//ERROR***

The userId's value is 29 and the departmentId's value is 17,

then I change code to:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://xxx.xxx.xx.xx/FMS/Pages/Service/FMService.svc/GetAnnouncement?userId=%@&departmentId=%@&pageIndex=%@&pageSize=%@",userId,departmentId,1,10]];//also the same error

There is also the same error...

and I dont know why, so pls help me with this, thank you.

Upvotes: 0

Views: 111

Answers (2)

Paul.s
Paul.s

Reputation: 38728

The last two argument to stringWithFormat: are not objects therefore you should not use the format specifier %@ instead use %d

 [NSString stringWithFormat:@"https://xxx.xxx.xx.xx/FMS/Pages/Service/FMService.svc/GetAnnouncement?userId=%@&departmentId=%@&pageIndex=%d&pageSize=%d",userId,departmentId,1,10]

Upvotes: 0

user529758
user529758

Reputation:

The last two format specifiers have to be %d to print/format an ordinal decimal number. %@ expects an object and tries to call its -description method, which fails for normal numbers (like 1 and 17) as object pointers. I'm surprised why the logging worked.

So the correct format string should be:

[NSString stringWithFormat:@"https://xxx.xxx.xx.xx/FMS/Pages/Service/FMService.svc/GetAnnouncement?userId=%@&departmentId=%@&pageIndex=%d&pageSize=%d",userId,departmentId,1,10];

Upvotes: 1

Related Questions