iPhone Dev - NSString Creation

后端 未结 2 1916
南方客
南方客 2021-01-28 05:14

I\'m really confused with NSStrings. Like when should I do

NSString *aString = @\"Hello\";

of should it be:

NSString *aString =         


        
2条回答
  •  暖寄归人
    2021-01-28 06:18

    In general you should do the first, but they are mostly functionally the same. You can treat constant NSStrings just like normal NSString string objects, for instance:

    [@"Hello" length]
    

    will return 5. You can assign them to properties, everything just works. The one thing you might notice is that with the constant NSStrings you don't have to worry about retain/release. That is because they are actually mapped into the applications readonly data section, and don't have allocated memory. Retain and release calls against them still work, they just become noops.

提交回复
热议问题