简体   繁体   English

客观-C。 非弧的弧库

[英]objective-C. Arc library in non-arc

I added PESGraph arc library to a project without arc. 我将PESGraph弧库添加到没有弧的项目中。

I have already found an answer that it is possible and it really works. 我已经找到了答案,它是可能的,它确实有效。 But how to handle memory for objects from arc library in non-arc project. 但是如何在非弧项目中处理来自弧库的对象的内存。 I mean at least alloc, retain, release. 我的意思是至少分配,保留,释放。

For example can I write release in dealloc method for object from arc if it was declared as retain property? 例如,如果声明为retain属性,我可以在aralloc方法中为对象写入arc吗?

If a file is compiled with ARC disabled, you can call release (and retain and autorelease ) in that file, and you are responsible for making those calls in the proper places. 如果在禁用ARC的情况下编译文件,则可以在该文件中调用release (和retain and autorelease ),并且您负责在适当的位置进行这些调用。

The idea of ARC is that, when ARC is enabled, the compiler inserts those calls for you. ARC的想法是,当启用ARC时,编译器会为您插入这些调用。 If you have ARC disabled for some of your files, then in those files you must insert the calls yourself. 如果您为某些文件禁用了ARC,则必须在这些文件中自行插入呼叫。

Cocoa has very strong conventions for when you need to retain , release , and autorelease objects if ARC is disabled. 如果禁用ARC,则需要retainreleaseautorelease对象时,Cocoa具有非常强的约定。 Read Cocoa Core Competencies: Memory Management to get started. 阅读Cocoa核心能力:内存管理入门。 Then look at the Advanced Memory Management Programming Guide if you need more details. 如果您需要更多详细信息,请查看高级内存管理编程指南 It's not really very advanced. 它并不是非常先进。

The compiler follows the same conventions when ARC is enabled. 启用ARC时,编译器遵循相同的约定。 That is why you can link ARC-enabled files and ARC-disabled files in the same executable. 这就是为什么您可以在同一个可执行文件中链接启用ARC的文件和ARC禁用的文件。

Memory management in Cocoa is completely local -- what memory management operations need to be performed in a function can be determined purely by looking at that function only, without caring about other code. Cocoa中的内存管理是完全本地的 - 需要在函数中执行的内存管理操作只能通过查看该函数来确定,而无需关心其他代码。 Each function can be considered independently in terms of memory management, as long as they all follow the rules. 每个功能都可以在内存管理方面独立考虑,只要它们都遵循规则。 ARC simply implements the rules (the same ones you would follow in MRC) automatically. ARC只是自动实现规则(您将在MRC中遵循的规则)。 Different parts of the code can use ARC or MRC independently, without affecting each other. 代码的不同部分可以独立使用ARC或MRC,而不会相互影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM