views:

84

answers:

1

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 with:

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.

+4  A: 

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