OC内存管理总结三:

来源:互联网 发布:网络摄像机组装 编辑:程序博客网 时间:2024/06/05 06:56

8.copy and mutableCopy

在新的Xcode中,用property关键字来代替setter和get方法,@property后面括号所跟的参数的含义如下:

1.atomicity: We will always use nonatomic for this attribute. There is rarely a reason to use the default, atomic, and the discussion of why is outside thes cope of this book.

2.writability :By default, a property is readwrite. A readwrite property will generate a setter and getter method. The other option, readonly, only generates a getter method.

3.memory management :This attribute category only applies to the setter method. By default, a property is assign(赋值). In this case, a property’s setter method only assigns the incoming argument to its instance variable. The other options are retain and copy, where the incoming argument is either

retained or copied and then assigned to the instance variable. (We’ll talk more about copy in a moment.)

memory management中的retain就不用说了,跟之前完善的设置器一样,这里要说说为什么有时候要用copy,下面先说一下copy和mutableCopy的区别。

copy:从原来的对象中复制出一个不可改变并且值跟原来的一样。副本和原本的地址是不同的,即值一样存放的位置不一样。还有一点就是副本不可改变如果你copy一个NSMutableArray,返回的副本是NSarray(When you copy an object, the copy returned is immutable. For instance, if you copy an
NSMutableArray, the new object is simply an NSArray)。

mutableCopy:返回的是一个可以改变的对象,那么为什么有的要用到copy呢?先来看看下面的代码:

NSMutableString *str = [[NSMutableString alloc] initWithString:@"White Sofa"];
// This is okay, as NSMutableString is a NSString since it is a subclass
[possession setPossessionName:str];
[str appendString:@" - Stained"];
// possession's name is now "White Sofa - Stained"

因为这是一个可变的字符串,可以在外面改变属性的值,这样的做法是不好的,我们一般通过setter来改变(体现封装性,也防止不小心直接改了),因为NSMutableString是NSString的子类,所以可以赋一个NSMutableString类型的值给NSString类型的属性。同理其他的类中如果有可变的子类如NSMutableArray也会产生”在外面直接被改变而不是通过setter改变“这种问题。为了解决这个问题我们在setter中改变如下:

- (void)setPossessionName:(NSString *)str
{
id t = [str copy];
[possessionName release];
possessionName = t;
}

变为这种写法后即使在外面改变传进来的str也不会改变属性的值,因为possessionName所指向的是另外一个地址,值还是和传进来的一样。

所以property关键字中的copy参数就是为了解决这个问题的。一般情况下如果属性的类型是一个有可变子类的类的对象时,要用到copy而不是retain。

9.关于堆(heap),数据块(data segment)和栈(stick)

除了堆之外还有两个部分的内存用于不同目的,分别是数据块和栈(There are actually two more parts of memory that serve different purposes: the data segment and the stack.)。(都是RAM)

数据块(data segment):存放可执行文件和方法,函数的指令。当应用程序启动时这块内存区域不会被改变,当代码加载进这块内存时就不能修改了(当程序启动时,所有的代码加载进来)。注意:建立一个NSString时如:NSString *foo = @"A string";这个字符串存在数据块当中。(the data segment and the stack. The data segment is where the application executable lives, and all of the instructions that make up your methods and functions live here. This area in memory never changes after an application is launched. The code is loaded into memory once and isn’t modified. When you create a literal NSString like so,
NSString *foo = @"A string";)

栈(stick):栈是为处理调用函数时预留的,当你调用函数时,一大块栈会预留给该函数,我们称这块栈为栈帧(stack frame)。栈帧保留调用方的地址为的是当函数执行完后能够返回之前调用它的函数,也保留一些传进来的参数,函数的返回值和局部变量。(The stack is an area of memory reserved for handling the calling of functions. (A method is really just a function under the hood.) When you call a function, a chunk of the stack is reserved specifically for that function. We call this chunk a stack frame. The stack frame holds the address of the function that called it so that when the function ends, it can return to the previous function. It also contains any of the arguments passed to the function and a space to store the return value of a function. Additionally, it reserves memory for the local variables of a function)

堆(heap):保存对象的实例变量(指针和一般类型如int),指针型的实例变量占四个字节。比如一个类有三个类型为对象和一个为int的属性,每个指针占四个再加上int的四个字节那就是16个字节了再加上父类的就是这个类所需要的堆内存(从堆中分配)。(A Possession has three pointers instance variables and one int, which are each 4 bytes. Therefore, the Possession is 16 bytes plus the amount of bytes an NSObject needs. Let’s pretend an NSObject is also 4, for a total of 20 bytes needed per Possession. So, when instantiating a Possession, 20 bytes are allocated from the heap. Even if the NSString that is the possessionName of that Possession is 100 bytes (because it has 100 characters), the Possession
itself remains 20 bytes. This can help us understand the difference between pointers to objects and objects themselves.)

0 0
原创粉丝点击