开发者

OBJC_MSGSEND Error in NSString stringWithFormat

I have a button in a test iPhone0 application that opens StackOverflow questions based on their GET ID in the URL. Every time the button is pressed, the page should reload to the next question.

I keep count of the GET ID through a int count initially set to 1 and incremented every button press.

Hard-coding the URL using: NSString *urlAddress=[NSString stringWithFormat:@"http://stackoverflow.com/questions/1"];

works, but obviously doesn't allow for use of the counter. When I try to implement the counter w开发者_如何学编程ith:

NSString *urlAddress =[NSString stringWithFormat: @"http://stackoverflow.com/questions/%@", count];

The program fails with the OBJC_MSGSEND error. Why does this line of code not work?

*I have debugged and this is the first line that causes said error.

Thanks.


count isn't an object. You need to use %d, not %@. Using %@ as a format specifier means "send a description method to the object I've provided as an argument". Since your variable count is not in fact an object, you can't send any messages to it.

Your code (shortened to show the example better), looks like this:

NSString *s = [NSString stringWithFormat:@"something/%@", count];

Which is (pretty much) equivalent to this:

NSString *s = [NSString stringWithFormat:@"something/%@", [count description]];

As you can imagine, the runtime can't make heads or tails of that (count being an int, after all). Using this format will work:

NSString *s = [NSString stringWithFormat:@"something/%d", count];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜