让旧工程支持iOS5 新的Automatic Reference Counter 语法
来源:互联网 发布:python for 重试 编辑:程序博客网 时间:2024/06/08 08:41
OS5 在 2011 WWDC 宣布
主要重点为 iCloud, iOS 5 及 Lion
其中对iOS 的开发者来说,除了新的API 之外(如iCloud API)
最重要的改变大概就是Automatic Reference Counter 及Storyboard 吧
尤其是 Automatic Reference Counter
可以让开发者免去retain/release/autorelease/dealloc 的动作,减少leak 的产生
对初学者来说,也可减少学习reference count 的困扰
虽然它和 Java 的 garbage collection 很像
目的都是在减少使用者出错,帮忙做记忆体管理的动作
但其实作法是不相同的
Java 是在runtime 时做资源回收的动作,发现有不需要用到的记忆体空间即自动回收
而iOS5 则是在compiler 时就帮使用者加上release 的动作
并非等到真正执行时才在背后执行资源回收的机制
新的XCode 4.2 (iOS5 beta) 同时支持新旧机制,你旧工程无需更改也可以compile
所以如果你的工程已完成,可以不用理它
但如果你的旧工程也想有这项方便的机制,就必须一些修改
建议无论如何还是学习一下
因为新版的XCode 预设的template 已经是使用新的语法了
变更设定以支持 Automatic Reference Counter
首先在Build Settings 里,必须把Compiler 改成Apple LLVM compiler 3.0
然后Automatic Reference Counting 标志也必须打开
这时候compile 应该会产生一堆错误了,不用担心
因为当设定Automatic Reference Counting 之后,就不支持旧的语法
像是retain/release/autorelease/dealloc 之类的语法全部都会错误
必须全部清除这些程式码。
首先开始修改 NSAutoreleasePool
通常这会出现在几个地方,一个是main.m 里
另外就是在Thread 里出现(如果你的程式有用到的话)
我们必须把 main.m 改成以下
用autoreleasepool 语法来取代原本的NSAutoreleasePool。
int main(int argc, char *argv[])
{
int retVal = 0;
@autoreleasepool
{
retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([xxxAppDelegate class]));
}
return retVal;
}
开始修改程式码了
@property (nonatomic, retain) 请改为@property (strong, nonatomic)
// 請改成
@property (strong, nonatomic) NSString *str;
接下来就没有捷径了,就是把所有的retain/release/autorelease/dealloc 全部找出来mark 掉
最快的方法就是搜寻dealloc 函式直接删掉它,就可以省掉一堆错误讯息了接下来就一边 compile 一边除错让compiler 帮你找出所有使用到旧语法的程式码,一个一个修正它修正完成后你再也不需要担心忘记release 任何变数了,因为新的compiler 都会帮你处理好好的了。
转自:http://www.richiestyle.org
- 让旧工程支持iOS5 新的Automatic Reference Counter 语法
- 让旧工程支持iOS5 新的Automatic Reference Counter 语法
- iOS5 Automatic Reference Counting
- Hypertable的automatic Counter
- Automatic Reference Counting(ARC)特性学习(iOS5新特性学习之五)
- Automatic Reference Counting(ARC)特性学习(iOS5新特性学习之五)
- 如何让旧浏览器支持HTML5新标签
- 如何让旧浏览器支持HTML5新标签
- 如何在Xcode5 上创建支持iOS5.0的工程
- 如何在Xcode5 上创建支持iOS5.0的工程
- Java7 新支持的语法
- iOS5的新特性
- xcode5 修改支持ios5以上工程
- 计时器 Counter 的新想法
- HTML5 - 使用HTML5Forms让旧浏览器支持表单新特性,表单验证
- 错误:'release' is unavailable: not available in automatic reference counting mode..(Xcode4.2.1+iOS5.0)
- 错误:'release' is unavailable: not available in automatic reference counting mode..(Xcode4.2.1+iOS5.0)
- 错误:'release' is unavailable: not available in automatic reference counting mode..(Xcode4.2.1+iOS5.0
- JAX-WS规范
- HttpModule的认识
- windows phone 7
- apt命令用法
- Windows 缓冲区溢出与数据执行保护DEP
- 让旧工程支持iOS5 新的Automatic Reference Counter 语法
- android 之 Service(Context BindService())
- Visual Studio 2008 常见错误解决 之 预编译头
- ie 下 javascript调试工具汇总
- WireShark 过滤语法
- 谈iPhone和iPad开发中的图标设置
- lucene中QueryParser的使用查询示例
- 二叉查找树 ADT实现
- wsdl2java命令