简体   繁体   English

目标C - 分配,复制,保留

[英]Objective C - Assign, Copy, Retain

I'm new to Objective C. I have basic knowledge in C, including the concept of pointers. 我是Objective C的新手。我有C的基本知识,包括指针的概念。 I have two basic questions: 我有两个基本问题:

  1. Can someone explain the difference between assign,copy, and retain with some analogy? 有人能解释分配,复制和保留之间的区别吗?
  2. How do you handle a function which returns pointer variable, and how do you perform messaging through a return pointer? 如何处理返回指针变量的函数,以及如何通过返回指针执行消息传递?

Updated Answer for Changed Documentation 更新了更改文档的答案

The information is now spread across several guides in the documentation. 现在,这些信息分散在文档中的几个指南中。 Here's a list of required reading: 这是一个必读的列表:

The answer to this question now depends entirely on whether you're using an ARC-managed application (the modern default for new projects) or forcing manual memory management. 现在,这个问题的答案完全取决于您是使用ARC管理的应用程序(新项目的现代默认设置)还是强制手动内存管理。

Assign vs. Weak - Use assign to set a property's pointer to the address of the object without retaining it or otherwise curating it; 赋值与弱 - 使用assign设置属性指向对象地址的指针,而不保留它或以其他方式策划它; use weak to have the property point to nil automatically if the object assigned to it is deallocated. 如果取消分配分配给它的对象,则使用weak使属性自动指向nil。 In most cases you'll want to use weak so you're not trying to access a deallocated object (illegal access of a memory address - " EXC_BAD_ACCESS ") if you don't perform proper cleanup. 在大多数情况下,如果不进行适当的清理,您将需要使用弱,因此您不会尝试访问已释放的对象(非法访问内存地址 - “ EXC_BAD_ACCESS ”)。

Retain vs. Copy - Declared properties use retain by default (so you can simply omit it altogether) and will manage the object's reference count automatically whether another object is assigned to the property or it's set to nil; 保留与复制 - 声明的属性默认使用retain (因此您可以完全省略它)并自动管理对象的引用计数,无论是将另一个对象分配给属性还是将其设置为nil; Use copy to automatically send the newly-assigned object a -copy message (which will create a copy of the passed object and assign that copy to the property instead - useful (even required) in some situations where the assigned object might be modified after being set as a property of some other object (which would mean that modification/mutation would apply to the property as well). 使用copy自动发送新分配的对象a -copy消息(它将创建传递的对象的副本并将该副本分配给属性 - 在某些情况下有用(甚至是必需的),在这种情况下,被分配的对象可能被修改后设置为某个其他对象的属性(这意味着修改/变异也适用于该属性)。

The Memory Management Programming Guide from the iOS Reference Library has basics of assign, copy, and retain with analogies and examples. iOS参考库中的“ 内存管理编程指南”具有通过类比和示例进行分配,复制和保留的基础知识。

copy Makes a copy of an object, and returns it with retain count of 1. If you copy an object, you own the copy. copy复制一个对象,并以retain count为1返回它。如果复制一个对象,则拥有该副本。 This applies to any method that contains the word copy where “copy” refers to the object being returned. 这适用于包含单词copy的任何方法,其中“copy”指的是要返回的对象。

retain Increases the retain count of an object by 1. Takes ownership of an object. retain将对象的保留计数增加1.取得对象的所有权。

release Decreases the retain count of an object by 1. Relinquishes ownership of an object. release减少对象的保留计数1.放弃对象的所有权。

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"First",@"Second", nil];
NSMutableArray *copiedArray = [array mutableCopy];
NSMutableArray *retainedArray = [array retain];

[retainedArray addObject:@"Retained Third"];
[copiedArray addObject:@"Copied Third"];

NSLog(@"array = %@",array);
NSLog(@"Retained Array = %@",retainedArray);
NSLog(@"Copied Array = %@",copiedArray);

array = (
    First,
    Second,
    "Retained Third"
)
Retained Array = (
    First,
    Second,
    "Retained Third"
)
Copied Array = (
    First,
    Second,
    "Copied Third"
)
  1. assign 分配

    • assign is a default property attribute assign是默认属性
    • assign is a property attribute tells the compiler how to synthesize the property's setter implementation assign是一个属性属性,告诉编译器如何合成属性的setter实现
  2. copy: 复制:

    • copy is required when the object is mutable 当对象是可变的时,需要复制
    • copy returns an object which you must explicitly release (eg, in dealloc) in non-garbage collected environments copy返回一个对象,您必须在非垃圾回收环境中显式释放(例如,在dealloc中)
    • you need to release the object when finished with it because you are retaining the copy 完成后需要释放对象,因为您保留了副本
  3. retain: 保留:

    • specifies the new value should be sent “-retain” on assignment and the old value sent “-release” 指定新值应该在赋值时发送“-retain”并且旧值发送“-release”
    • if you write retain it will auto work like strong 如果你写保留它将自动工作像强
    • Methods like “alloc” include an implicit “retain” 像“alloc”这样的方法包含一个隐含的“保留”

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

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