ios 目标 C - 分配、复制、保留
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4510913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective C - Assign, Copy, Retain
提问by Sabha B
I'm new to Objective C. I have basic knowledge in C, including the concept of pointers. I have two basic questions:
我是 Objective C 的新手。我有 C 的基本知识,包括指针的概念。我有两个基本问题:
- Can someone explain the difference between assign,copy, and retain with some analogy?
- How do you handle a function which returns pointer variable, and how do you perform messaging through a return pointer?
- 有人可以用一些类比来解释分配、复制和保留之间的区别吗?
- 您如何处理返回指针变量的函数,以及如何通过返回指针执行消息传递?
回答by Joshua Nozzi
Updated Answer for Changed Documentation
更改文档的更新答案
The information is now spread across several guides in the documentation. Here's a list of required reading:
该信息现在分布在文档中的多个指南中。以下是必读的清单:
- Cocoa Core Competencies: Declared property
- Programming with Objective-C: Encapsulating Data
- Transitioning to ARC Release Notes
- Advanced Memory Management Programming Guide
- Objective-C Runtime Programming Guide: Declared Properties
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 assignto set a property's pointer to the address of the object without retaining it or otherwise curating it; use weakto have the property point to nil automatically if the object assigned to it is deallocated. In most cases you'll want to use weakso 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.
分配与弱- 使用分配将属性的指针设置为指向对象的地址,而不保留它或以其他方式管理它;如果分配给它的对象被释放,则使用weak使属性自动指向 nil。在大多数情况下,您会希望使用weak,这样EXC_BAD_ACCESS
如果您没有执行适当的清理,您就不会尝试访问已释放的对象(非法访问内存地址 - “ ”)。
Retain vs. Copy- Declared properties use retainby 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; Use copyto 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).
Retain vs. Copy- 声明的属性默认使用retain(所以你可以完全省略它)并且会自动管理对象的引用计数,无论另一个对象是分配给该属性还是设置为nil;使用copy自动向新分配的对象-copy
发送消息(这将创建传递对象的副本并将该副本分配给属性 - 在某些情况下很有用(甚至需要),在某些情况下,分配的对象可能在设置为某个其他对象的属性(这意味着修改/变异也适用于该属性)。
回答by Larry Hipp
The Memory Management Programming Guidefrom the iOS Reference Library has basics of assign, copy, and retain with analogies and examples.
iOS 参考库中的Memory Management Programming Guide包含分配、复制和保留的基础知识,并通过类比和示例。
copyMakes a copy of an object, and returns it with retain count of 1. If you copy an object, you own the copy. This applies to any method that contains the word copy where “copy” refers to the object being returned.
retainIncreases the retain count of an object by 1. Takes ownership of an object.
releaseDecreases the retain count of an object by 1. Relinquishes ownership of an object.
copy创建一个对象的副本,并以保留计数 1 返回它。如果您复制了一个对象,则您拥有该副本。这适用于任何包含单词 copy 的方法,其中“copy”指的是被返回的对象。
保留将对象的保留计数增加 1。获得对象的所有权。
release将对象的保留计数减少 1。放弃对象的所有权。
回答by srivas
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"
)
回答by Chen Rui
assign
- assign is a default property attribute
- assign is a property attribute tells the compiler how to synthesize the property's setter implementation
copy:
- copy is required when the object is mutable
- copy returns an object which you must explicitly release (e.g., in dealloc) in non-garbage collected environments
- you need to release the object when finished with it because you are retaining the copy
retain:
- specifies the new value should be sent “-retain” on assignment and the old value sent “-release”
- if you write retain it will auto work like strong
- Methods like “alloc” include an implicit “retain”
分配
- 分配是默认的属性属性
- assign 是一个属性属性,告诉编译器如何合成属性的 setter 实现
复制:
- 对象可变时需要复制
- copy 返回一个必须在非垃圾收集环境中显式释放(例如,在 dealloc 中)的对象
- 完成后您需要释放对象,因为您保留了副本
保持:
- 指定新值应该在赋值时发送“-retain”,旧值发送“-release”
- 如果你写保留它会自动像强一样工作
- 像“alloc”这样的方法包括一个隐含的“retain”