Xcode 4.2的编译改动(ARC)

本文深入探讨了ARC(Automatic Reference Counting)的概念及其在Objective-C中的应用。解释了ARC如何简化内存管理过程,避免手动调用retain和release方法,并介绍了强引用(strong)与弱引用(weak)的区别及应用场景。此外,还讨论了@autoreleasepool的使用方式,以及在使用ARC时需要注意的一些限制。

关于ARC

ARCAutomatic Reference Counting)是一种据说可以自动释放内存的方式,但带来了不少弊端,例如:

·如果结构复杂一些的view,系统可能会释放错误。

·由于内存是自动释放,所以retain/release/autorelease/dealloc也不能使用了(也不需要使用了),如果使用,反而会报错,如

ARC forbids explicit message send of release

或者

'release' is unavailable: not available in automatic reference counting mode..

显然ARCrelease是不能共存的,想要编译通过就要干掉一个。

通常被干掉的会是ARC

在老版的xcode中,打开“Build Setting”,找到“Objective-C Automatic Reference Counting”项,将它的值设置成“NO”

在较新的Xcode中如4.2,打开“Build Setting”,找到"CLANG_ENABLE_OBJC_ARC" 将它的值设置成NO

关于strongweak

强引用与弱引用的广义区别:

强引用strong也就是我们通常所讲的引用,其存亡直接决定了所指对象的存亡。如果不存在指向一个对象的引用,并且此对象不再显示列表中,则此对象会被从内存中释放。

弱引用weak除了不决定对象的存亡外,其他与强引用相同。即使一个对象被持有无数个若引用,只要没有强引用指向他,那麽其还是会被清除。没办法,还是强哥有面子。

 

简单地讲,

@property那里用strong代替retain,用weak代替assign

weakassign多了一个功能,当对象消失后自动把指针变成nil,好处不言而喻。 

关于@Autoreleasepool

NSAutoReleasePool   @ {Autoreleasepool / / Code for autoreleasepool } block 取代了。所起的作用还是一致的。 

附上官方原文:

Automatic Reference Counting

Automatic Reference Counting (ARC) is a compiler-level feature that simplifies the process of managing the lifetimes of Objective-C objects. Instead of you having to remember when to retain or release an object, ARC evaluates the lifetime requirements of your objects and automatically inserts the appropriate method calls at compile time. 

To be able to deliver these features, ARC imposes some restricti*****—primarily enforcing some best practices and disallowing some other practices:

Do not call the retain, release, autorelease, or dealloc methods in your code.

In addition, you cannot implement custom retain or release methods. 

Because you do not call the release method, there is often no need to implement a custom dealloc method—the compiler synthesizes all that is required to relinquish ownership of instance variables. You can provide a custom implementation of dealloc if you need to manage other resources.

Do not store object pointers in C structures.

Store object pointers in other objects instead of in structures.

Do not directly cast between object and nonobject types (for example, between id and void*).

You must use special functi***** or casts that tell the compiler about an object’s lifetime. You use these to cast between Objective-C objects and Core Foundation objects.

You cannot use NSAutoreleasePool objects.

Instead, you must use a new @autoreleasepool keyword to mark the start of an autorelease block. The contents of the block are enclosed by curly braces, as shown in the following example 

@autoreleasepool

{

  // Your code here

}

ARC encourages you to think in terms of object graphs, and the relati*****hips between objects, rather than in terms of retain and release. For this reason, ARC introduces new lifetime qualifiers for objects, including zeroing weak references. The value of a zeroing weak reference is automatically set to nil if the object to which it points is deallocated. There are qualifiers for variables, and new weak and strong declared property attributes, as illustrated in the following examples:

// The following declaration is a synonym for: @property(retain) MyClass *myObject;

@property(strong) MyClass *myObject;

// The following declaration is similar to "@property(assign) MyOtherClass *delegate;"

// except that if the MyOtherClass instance is deallocated,

// the property value is set to nil instead of remaining as a dangling pointer

@property(weak) MyOtherClass *delegate;

Xcode provides migration tools to help convert existing projects to use ARC. For more information about how to perform this migration, see Xcode New Features User Guide. For more information about ARC itself, see Programming With ARC Release Notes.


【事件触发一致性】研究多智能体网络如何通过分布式事件驱动控制实现有限时间内的共识(Matlab代码实现)内容概要:本文围绕多智能体网络中的事件触发一致性问题,研究如何通过分布式事件驱动控制实现有限时间内的共识,并提供了相应的Matlab代码实现方案。文中探讨了事件触发机制在降低通信负担、提升系统效率方面的优势,重点分析了多智能体系统在有限时间收敛的一致性控制策略,涉及系统模型构建、触发条件设计、稳定性与收敛性分析等核心技术环节。此外,文档还展示了该技术在航空航天、电力系统、机器人协同、无人机编队等多个前沿领域的潜在应用,体现了其跨学科的研究价值和工程实用性。; 适合人群:具备一定控制理论基础和Matlab编程能力的研究生、科研人员及从事自动化、智能系统、多智能体协同控制等相关领域的工程技术人员。; 使用场景及目标:①用于理解和实现多智能体系统在有限时间内达成一致的分布式控制方法;②为事件触发控制、分布式优化、协同控制等课题提供算法设计与仿真验证的技术参考;③支撑科研项目开发、学术论文复现及工程原型系统搭建; 阅读建议:建议结合文中提供的Matlab代码进行实践操作,重点关注事件触发条件的设计逻辑与系统收敛性证明之间的关系,同时可延伸至其他应用场景进行二次开发与性能优化。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值