objective-c 2.0 入门笔记

转自:http://blog.sina.com.cn/s/blog_976270ec0100wopi.html

在新发布的ios5中,有个很吸引眼球的特性就是“Automatic Reference Counting”,简单来说就是内存自动回收,看起来似乎是平时开发中遇到的各种内存问题的福音,仔细了解了一下,似乎没有那么美好:
1。 需要iOS5才支持
2。 很多引用的类库暂时还不支持.麻烦的是对大多数第三方库需要加禁用arc的编译flag,因为大多都因为兼容性还在使用手动内存管理。
由于 iOS5 xcode4.2 引入了ARC (AutomicReferenceCounting) , 使用xcode4.2 新建项目时,会有一些小小的变化。 NSAutoReleasePool 被 @ {Autoreleasepool / / Code for autoreleasepool } block 取代了。
新增了__strong , __weak 编译指令。
ios5.0知道你的变量在何时需要release,何时需要autorelease,所以变量不要写成全局的,全局的变量用single去代替。
而且当你开启了ARC后, 如果使用传统的手工 release 就是报错 'release' is unavailable: not available in automatic reference counting modeAutomatic Reference Counting forbids explicit message send of 'release'
你可以按照如下方法关闭它
Xcode 4.2: 点击项目后,
Build Settings -> Apple LLVM compiler 3.0 - Language -> Objective-C Automatic Referencing Counting, 默认是YES 设置为NO即可。
[attachment=385]
但实际上 ARC确实比手动释放快,而且不会发生内存泄漏的情况,具体可以参考这里
http://clang.llvm.org/docs/AutomaticReferenceCounting.html
http://stackoverflow.com/questions/6385212/how-does-the-new-automatic-reference-counting-mechanism-work
一篇深入讨论 ARC的文章
http://longweekendmobile.com/2011/09/07/objc-automatic-reference-counting-in-xcode-explained/

 



posted @ 2012-05-07 18:10  good fortune  阅读(157)  评论(0编辑  收藏  举报