对ios应用程序中的内存保留有一些疑问?

发布于 2024-12-26 07:16:48 字数 264 浏览 4 评论 0原文

  1. 在 ios 应用程序中保留 NSObject 需要什么?

  2. retainCount==1retainCount=2、.....等有什么区别?

  3. 属性如何处理实例变量的保留和释放?

  4. 当我保留/释放 NSObject 时,创建/减少的保留计数减一,内存中到底发生了什么?

  1. What is the need for retain an NSObject in ios application?

  2. What is the difference between retainCount==1,retainCount=2,.....etc?

  3. How properties can handle retaining and releasing instance variable?

  4. when i am retain/releasing NSObject ,the retain count increate/decrease by one, what happens exactly in memory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故乡的云 2025-01-02 07:16:48
  1. 保留一个对象表明您正在获得该对象的所有权。所以你必须在完成后释放它一次。
  2. keepCount = 1 表示它正在从一处被强引用。如果retainCount = 2,则它从两个地方被强引用。
  3. 属性根据它是保留、复制还是分配属性,以不同的方式处理内存管理。
  4. 保留计数仅告诉我们该对象有多少个字符串引用。只要 ling 大于 0,该对象就不会从内存中删除。一旦它变成0,obj就会从内存中删除(调用该对象的dealloc)

编辑

  1. 如果它是re​​tain属性,每次使用'.'设置retain属性,那么旧值被释放,新值被保留并分配给该属性。复制也会发生同样的情况,只是新值被发送为副本而不是保留。如果是分配属性,则直接将新值赋给该属性(不释放,不保留)
  1. Retaining an object indicates that you are taking ownership of that object. So you have to release it once after you are done with it.
  2. retainCount = 1 indicates that it is being strongly referenced from one place. If retainCount = 2, then it is being strongly referenced from two places.
  3. Properties, depending on whether it is a retain, copy or assign property, handles memory management differently
  4. Retain count only tells us how many string references are there for the object. As ling as it is greater than 0, the object is not removed from memory. Once it becomes 0, the obj is removed from memory (dealloc of tht object is called)

EDIT:

  1. If it is a retain property, each time you set a retain property using the '.', then the old value is released, new value is retained and assigned to the property. The same happens with copy, just that the new value is sent a copy instead of retain. If it is an assign property, the new value is directly assigned to the property (no release, no retain)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文