Cat grooming Issues: Copy, object custom copy

Source: Internet
Author: User

Cat Share, must boutique
Original address: Http://blog.csdn.net/u013357243?viewmode=contents

The positive purpose of copycopy

Copy purpose: Create a copy, modify each other, do not disturb
Copy (immutable) and Mutablecopy (mutable) data types for the foundation framework.
For a custom class, copy is mutable.

Copy of a variable group

Here we use Nsmutablearray as an example.

//variable = "Variable & immutable, memory address will changevoidCopyDemo1 () {Nsmutablearray*arraym = [Nsmutablearrayarraywithobjects:@ (1), @(2),Nil];NSLog(@"%@%p%@", Arraym, Arraym, Arraym. Class);//1. variable = variable    Nsmutablearray*am = [Arraym mutablecopy];NSLog(@"%@%p%@", am, AM, am. Class);//2. Variable = immutable    Nsarray*a = [Arraym copy];NSLog(@"%@%p%@", A, a, a. Class);}

From the result we get a conclusion
1:arraym,am,a is a three different object
2: variable = variable (arraym is variable, with mutablecopy to AM to draw Am.class is Nsarraym)
3: variable = immutable (AM is variable, with copy to a a.class is Nsarrayi)

Copy of an immutable group
void copyDemo2(){ NSArray *array = @[@(1), @(2)]; NSLog(@"%@ %p %@", array, array, array.class); // 1. 不可变 => 可变 NSMutableArray *aM = [array mutableCopy]; NSLog(@"%@ %p %@", aM, aM, aM.class); // 2. 不可变 => 不可变(浅复制) // 指针的复制,引用计数+1 NSArray *a = [array copy]; NSLog(@"%@ %p %@", a, a, a.class);}

From the above results we get the conclusion
1: Immutable = variable memory address changed, class by Nsarrayi=>nsarraym
2: Immutable = immutable memory address consistent, class by Nsarrayi=>nsarrayi
The function is the copy of the pointer, reference count +1. Terminology (Shallow copy)

3:浅复制:不可变=>不可变   深复制:其他三种情况!   (在这里记忆时候记住都不能改就是浅复制,能改就是深复制,相对于记忆四中情况更容易记忆。)
Where do you use the most?

The most used here

@property (nonatomicNSString *name;

NSString
Block

都使用copy属性,copy属性,在赋值时,会默认做一次copy操作,让他变成“不可变的类型”strong相当于MRC中的retain,在赋值时,只是引用计数+1

What type of Yang is used in @property, and what kind of method he has.
For example, this code:

nsmutablestring *STRM = [nsmutablestring stringwithstring:@"Zhangsan"];NSLog (@"%@%p%@", StrM, StrM, StrM. Class);Person *p = [[Person alloc] init];P. Name= StrM;NSLog (@"%@%p%@"P. NameP. NameP. Name. Class);Modify"Source"P. NameWill follow the changes [StrM setstring:@"Lisi"];NSLog (@"= =%@%p", StrM, StrM);NSLog (@"%@%p"P. NameP. Name);

If you use strong to explain the name of person, when we modify STRM, P.name will follow, if the use of copy, then he will not be modified. This depends on the @property in the person class.
Simple to understand, if copy is used to copy a string to name
And if you use strong, it's just a reference count of +1.
Can p.name change it? Here we can not directly modify, we need to use a universal pointer.

// 修改p.name    id obj = p.name;    [obj setString:@"wangwu"];    NSLog(@"==== %@ %p", strM, strM);    NSLog(@"%@ %p", p.name, p.name);

This can be changed with strong, so the class will become unsafe. Let's change strong to copy.
When we modify the "source",

setString:@"lisi"];

The p.name is still intact.
This time the method of modifying the properties of the compiler is not a problem, but the runtime will error

Attempt to mutate immutable object with XXX
View modifies an immutable type, using the method xxx

For a "mutable type" property, do not use the Copy Descriptor definition, or the assignment is immutable!

Copy custom object [object Copy]

You want the object to be able to use the Copy method [P copy]
(custom object to implement copy function)
1> Compliance with nscopying protocol (essentially handy when programmers write code, quick Tips)
2> implementation-(ID) Copywithzone: (Nszone *) zone
(zone, zone, rarely used)
All copy methods will eventually call the Copywithzone method.
Copy operations an object and copies it to a new object.

- (id)copyWithZone:(NSZone *)zone{    // 1> 实例化对象,self 是对象    // self.class能够保证继承的子类同样使用copy方法    Person *p = [[self.class alloc] init];    // 2> 给属性赋值    p.nameself.name;    p.ageself.age;    // 3> 返回新对象    return p;}

That's when we call

void copyDemo5(){    Person *p = [[Person alloc] init];    p.name = @"zhangsan";    p.age18;    NSLog(@"%@", p);    Person *p1 = [p copy];    p1.name = @"lisi";    NSLog(@"%@", p1);}


At this point we can invoke the copy method of the class object we have defined, and can produce an object.
Note: P1.name = @ "Lisi" here is not a modification, but a re-assignment. Don't let copy get confused.

PS: New iOS Exchange Learning Group: 304570962
You can add a cat qq:1764541256 or Znycat.
Let's study hard together.
Original: Http://blog.csdn.net/u013357243?viewmode=contents

Cat grooming Issues: Copy, object custom copy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.