Objective-c copy mutableCopy copy object

Source: Internet
Author: User

Not all objects support copy.
Only classes that comply with the NSCopying protocol can send copy messages.
Only classes that comply with the NSMutableCopying protocol can send mutableCopy messages.

If a copy or mutableCopy message is sent without complying with the appeal protocol, an exception occurs.

By default, nsobject does not comply with the two Protocols.
However, the copy and mutableCopy methods are defined by nsobject.

To customize copy, you must comply with NSCopying and implement copyWithZone: Method
To customize mutableCopy, you must comply with NSMutableCopying and implement mutableCopyWithZone: Method

I have read several basic core classes that comply with the NSCopying protocol.
For example, NSString NSNumber

After the copy operation, a new class is returned, and you are responsible for releasing it. The previously copied retaincount is not greater than 1. Therefore, you do not need to release it.

Copy and mutableCopy are objects that cannot be modified after copy is returned. mutableCopy returns objects that can be modified.

The following is an example of copyWithZone implementation.
@ Interface BankAccount: NSObject
{
Double accountBalance;
Long accountNumber;
}
-(Void) setAccount: (long) y andBalance: (double) x;
-(Double) getAccountBalance;
-(Long) getAccountNumber;
-(Void) setAccountBalance: (double) x;
-(Void) setAccountNumber: (long) y;
-(Void) displayAccountInfo;
-(Id) copyWithZone: (NSZone *) zone;
@ End


-(Id) copyWithZone: (NSZone *) zone
{
BankAccount * accountCopy = [[BankAccount allocWithZone: zone] init];

[AccountCopy setAccount: accountNumber andBalance: accountBalance];
Return accountCopy;
}

Deep copy and shallow copy
The above method is a shallow copy, which means that the BankAccount class memory is re-allocated and the memory is not re-allocated for the BankAccount attribute.
The attributes of both bankaccounts point to the same place. If one of the attributes of the BankAccount is modified, the other is the same.
Will also change together

Deep copy
An archive function is used for deep copy. First, the original archive (in fact, the object is serialized, and then saved to a file. In other words, the archive can be deserialized and assigned to another object ), and save it to another object.
NSKeyedArchiver serialization class
NSKeyedUnarchiver deserialization class



NSArray * myArray1;
NSArray * myArray2;
NSMutableString * tmpStr;
NSMutableString * string1;
NSMutableString * string2;
NSMutableString * string3;
NSData * buffer;


String1 = [NSMutableString stringWithString: @ "Red"];
String2 = [NSMutableString stringWithString: @ "Green"];
String3 = [NSMutableString stringWithString: @ "Blue"];

MyArray1 = [NSMutableArray arrayWithObjects: string1, string2, string3, nil];

Buffer = [NSKeyedArchiver archivedDataWithRootObject: myArray1];
MyArray2 = [NSKeyedUnarchiver unarchiveObjectWithData: buffer];

TmpStr = [myArray1 objectAtIndex: 0];

[TmpStr setString: @ "Yellow"];

NSLog (@ "First element of myArray1 = % @", [myArray1 objectAtIndex: 0]);
NSLog (@ "First element of myArray2 = % @", [myArray2 objectAtIndex: 0]);


There are also three types of cocoa attributes
Protected-Access is allowed on Ly by methods of the class and any subclasses. the default value is this
Private-Access is restricted to methods of the class. Access is not available to subclasses.
Public-Direct access available to methods of the class, subclasses and co De in other module files and classes.
Public variables can be accessed using the-> operator.

Reference: http://www.techotopia.com/index.php/Copying_Objects_in_Objective-C
Http://wenku.baidu.com/view/12d6592fb4daa58da0114af0.html
Http://www.techotopia.com/index.php/Objective-C_-_Da Ta_Encapsulation, _ Synthesized_Accessors_and_Dot_Notation

Detailed documentation on accessors
Http://cocoacast.com /? Q = node/103

Related Article

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.