1
我必須使用scanf()
來接受NSString
對象中的用戶名。
如果我使用%s
那麼它會顯示我錯誤。我的代碼是:如何使用scanf接受NSString中的值?
NSString *name;
scanf("%s",&name); //It is showing error
scanf("%@",&name); //It also show me error
我該怎麼辦?
我必須使用scanf()
來接受NSString
對象中的用戶名。
如果我使用%s
那麼它會顯示我錯誤。我的代碼是:如何使用scanf接受NSString中的值?
NSString *name;
scanf("%s",&name); //It is showing error
scanf("%@",&name); //It also show me error
我該怎麼辦?
您不能使用scanf()
,因爲它不能以NSString
對象作爲參數。您可以先掃描char *
c字符串中的字符,然後用 - (id)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding
從中創建NSString
。
誰說使用scanf? – Krunal 2012-03-30 09:27:28
您將'name'聲明爲指針,然後使用該指針的指針作爲參數。即使使用舊的C字符串也不行。 – 2012-03-30 09:31:34
[使用scanf與NSStrings]可能的重複(http://stackoverflow.com/questions/3220823/using-scanf-with-nsstrings) – sch 2012-03-30 09:34:40