2013-08-24 35 views
0

我試圖用「多個部分」製作NSURL。我有多個文件,其名稱和編號如下:john2.mp3adam13.mp3。我隨機化了一個數字和一個名字。然後我想加載文件:NSURL中的多個參數

url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%s%d.mp3",[[NSBundle mainBundle] resourcePath]], name, number]; 

這不起作用。它說:Too many arguments in one method。我究竟做錯了什麼?

我使用Xcode 4.6併爲iOS 6.1開發。

回答

2

你在錯誤的地方有一個方括號。它應該是:

url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%s%d.mp3",[[NSBundle mainBundle] resourcePath], name, number]]; 

而且,如果nameNSString,那麼你需要使用%@。如果name確實是char *,則使用%s是正確的。

它會更清楚這樣做,而不是:

NSString *filename = [NSString stringWithFormat:@"%@%d", name, number]; 
NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:@"mp3"]; 

不要嘗試如此多的嵌套調用塞進一條線。分開來。閱讀和調試更容易。

+0

工作得很好!非常感謝! – user2059738