NSString初始化及其記憶體管理,nsstring初始化

來源:互聯網
上載者:User

NSString初始化及其記憶體管理,nsstring初始化

1. NSString 3種初始化方法

a. 直接賦值: NSString *str = @"a string", 這種方式使用常量記憶體。

b. 使用類方法: NSString *str = [NSString stringWithString:@"a string"];

          NSString * str = [NSString stringWithFormat:@"age is %i, height is %f", 20, 180];

c. 使用執行個體方法: NSString *str = [[NSString alloc] initWithString:@"a string"];

          NSString *str  [[NSString alloc] initWithFormat:@"age is %i, height is %f",20,180];

 

2. NSString 記憶體管理

使 用Xcode 6.1.1 測試建立新的NSString,發現原來在Xcode4.6下沒有警告的的initWithString和stringWithString都會有如下警 告出現,度娘發現已有答案,原因是因為新版的Xcode已經做了最佳化,意思是這種寫法是多餘的,建議直接 ’NSString *str = @"";‘這樣的格式來寫, 這樣str的記憶體會直接從常量記憶體區開闢。

參考連結:http://www.cnblogs.com/celestial/archive/2012/10/10/2719169.html

         http://www.cnblogs.com/hellocby/archive/2012/08/23/2652201.html  

 

待補充...

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.