2012-03-30 23 views
1

我必須使用scanf()來接受NSString對象中的用戶名。
如果我使用%s那麼它會顯示我錯誤。我的代碼是:如何使用scanf接受NSString中的值?

NSString *name; 
scanf("%s",&name); //It is showing error 
scanf("%@",&name); //It also show me error 

我該怎麼辦?

+2

誰說使用scanf? – Krunal 2012-03-30 09:27:28

+0

您將'name'聲明爲指針,然後使用該指針的指針作爲參數。即使使用舊的C字符串也不行。 – 2012-03-30 09:31:34

+0

[使用scanf與NSStrings]可能的重複(http://stackoverflow.com/questions/3220823/using-scanf-with-nsstrings) – sch 2012-03-30 09:34:40

回答

3

您不能使用scanf(),因爲它不能以NSString對象作爲參數。您可以先掃描char * c字符串中的字符,然後用 - (id)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding從中創建NSString

相關問題