为什么在启用ARC的项目中不需要维护保留计数 [英] Why maintaining retain count is not required in ARC enabled project

查看:184
本文介绍了为什么在启用ARC的项目中不需要维护保留计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

保留计数是在Objective-C中管理内存的方式。当您创建一个对象时,它的保留计数为1.当您向对象发送保留消息时,其保留计数会增加1,我们知道ARC会自动执行此操作,但它如何执行它所使用的技术? ?

Retain counts are the way in which memory is managed in Objective-C. When you create an object, it has a retain count of 1. When you send an object a retain message, its retain count is incremented by 1, which we know that ARC does it automatically but how it does what is the technique it use??

我仍然想知道内存管理是否自动完成,那么为什么有时我们会在对象分配或检索时遇到错误的访问错误。

And I still wonder if memory management is done automatically then why sometimes we get bad access error for objects allocations or retrieval.

我已经浏览过这个链接: - https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

I have already gone through this link:- https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

推荐答案

我认为ARC(由编译器在编译时完成,通过在'必要'中插入retain / release命令完成)依赖于变量的范围,即定义它们的代码块(即初始化)并且如果它的值存储在另一个变量中范围比初始变量的范围更广。

I think ARC (done by the compiler at compilation time, by inserting retain/release command where 'necessary') relies on the scope of variable, the block of code where they are defined (i.e. initialized) and if its value is stored in another variable whose scope is broader than the initial variable's scope.

这就是为什么你必须更准确地声明变量访问和存储的类型:通过变量通知编译器你的意图。

That's why you have to declare more precisely the type of variable access and storage: to inform the compiler your intentions with a variable.

但我也认为ARC看不到当前文件。 ARC对于全局变量和文件间依赖性更加棘手。

But I think too that ARC can't see further than the current file. ARC is more tricky with global variables and inter-files dependencies.

因此,Apple是一个更复杂的变量声明语法,用于替换非常简单的(IMO)保留/释放模式。所以开发人员不必担心内存管理。

So, Apple a more complex variable's declaration grammar to replace a very simple (IMO) retain/release pattern. So developers don't have to worry about memory management.

这使得许多开发人员可以访问管理语言(如Web开发人员)来开发Apple生态系统对于iOS。

That enable Apple ecosystem to be accessible to a lot more developers used to managed languages (like web developers) to develop for iOS.

我认为让开发人员相信你可以高效地开发而不必理解IT中的内存管理这样的基本概念是错误的。

I think it is a mistake to make developers believe you can develop efficiently without having to understand such a fundamental concept in IT as memory management.

但iOS的开发人员越多意味着开发的程序越多,活动期间的生态系统就越稳定,因此苹果的收入增加:-)

But more developers for iOS means more programs developed and a more stable ecosystem in term of activity, so more revenues for Apple :-)

这篇关于为什么在启用ARC的项目中不需要维护保留计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆