2009-11-11 53 views
0

我在測試iPhone0應用程序中有一個按鈕,它根據URL中的GET ID打開StackOverflow問題。每次按下按鈕,頁面都應該重新加載到下一個問題。NSString中的OBJC_MSGSEND錯誤stringWithFormat

我通過最初設置爲1的int count保持GET ID的計數,並且每按下一次按鈕就增加一次。

硬編碼使用的網址:NSString *urlAddress=[NSString stringWithFormat:@"http://stackoverflow.com/questions/1"];

作品,但顯然不允許使用計數器。當我試着使用來實現計數器:

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

程序無法與OBJC_MSGSEND錯誤。爲什麼這行代碼不起作用?

*我已調試,這是導致所述錯誤的第一行。

謝謝。

回答

4

count不是一個對象。您需要使用%d,而不是%@。使用%@作爲格式說明符意味着「發送一個description方法到我作爲參數提供的對象」。由於變量count實際上不是一個對象,因此無法向其發送任何消息。

您的代碼(簡稱顯示的例子更好),看起來是這樣的:

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

這是(幾乎)等價於:

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

正如你可以想像,在運行時(count畢竟是int)。使用這種格式將工作:

NSString *s = [NSString stringWithFormat:@"something/%d", count]; 
相關問題