2012-02-29 124 views
0

我無法將參數轉換爲我的Cocoa應用程序的NSString格式。我開始我的應用程序,像這樣:將char轉換爲NSString

open my.app --args a1 a2 

我嘗試訪問像這樣的論點:

const char *h_path_char = [[[[NSProcessInfo processInfo] arguments] objectAtIndex:1] fileSystemRepresentation]; 
const char *s_path_char = [[[[NSProcessInfo processInfo] arguments] objectAtIndex:2] fileSystemRepresentation]; 

NSString *h_path = [NSString stringWithUTF8String:h_path_char]; 
NSString *s_path = [NSString stringWithUTF8String:s_path_char]; 

NSLog(@"%s", h_path); 
NSLog(@"%s", s_path); 

然而,Xcode的埋怨NSLog以下警告:

轉換指定鍵入「char」,但參數的類型爲「NSString」。

我該如何克服這一點?

+3

不應該是'NSLog(@「%@」,h_path);'? – 2012-02-29 17:26:25

+0

@Robert,是的,或者用'%s'打印原始'const char *'。 – mpontillo 2012-02-29 17:27:23

回答

5

%s適用於C字符串。您應該使用%@而不是%s來輸出NSString(和其他基礎類型)到NSLog

+0

謝謝ayoy--我犯了一個菜鳥的錯誤。 – Abs 2012-02-29 18:53:14