IOS Get network picture size change Picture color value Grayscale What's The method collection

Source: Internet
Author: User

First, the basic concept of copying objects

Copy an object as a copy and open up a new piece of memory to store the replica object.

Second, if an object wants to have the function of copying, must implement <NSCopying> Agreement and <NSMutableCopying> Agreement

NSObject's Common objects are: NSNumber, NSString, Nsarray, Nsdictionary, Nsmutablearray, Nsmutabledictionay, NSMutableString, Copy-generated object is immutable, mutablecopy the resulting object when the variable

Third, the difference between retain and copy

    @autoreleasepool {        Nsmutablearray *array=[nsmutablearray arraywithobjects:@ "One", @ "one", @ "three", @ "four", nil] ;        Nsmutablearray *retainarray=[array retain];        [Retainarray Removelastobject];        For (NSString *str in array)        {            NSLog (@ "The part is%@", str);        }        NSLog (@ "The Retaincount is%ld", [Retainarray Retaincount]);        Insert Code        here ... NSLog (@ "Hello, world!");            }

Results:

2014-05-19 10:58:22.639 objective[1095:303] The part was one2014-05-19 10:58:22.641 objective[1095:303] the part is two2014 -05-19 10:58:22.641 objective[1095:303] The part was three2014-05-19 10:58:22.641 objective[1095:303] The Retaincount is 2

Nsmutablearray *array=[nsmutablearray arraywithobjects:@ "One", @ "one", @ "three", @ "four", nil];        Nsmutablearray *retainarray=[array mutablecopy];        [Retainarray Removelastobject];        For (NSString *str in array)        {            NSLog (@ "The part is%@", str);        }        NSLog (@ "The Retaincount is%ld", [Retainarray Retaincount]);


Results

2014-05-19 10:59:03.826 objective[1104:303] the part is one

2014-05-19 10:59:03.828 objective[1104:303] The part is the

2014-05-19 10:59:03.828 objective[1104:303] the part is three

2014-05-19 10:59:03.829 objective[1104:303] the part was four

2014-05-19 10:59:03.829 objective[1104:303] The Retaincount is 1


Iv. the difference between copy and mutablecopy

Copy returns a copy of an immutable object, Mutalbecopy returns a copy of a mutable object.

        Nsarray *array=[nsarray arraywithobjects:@ "One", @ "one", nil];        Nsmutablearray *array1=[array copy];        [Array1 addobject:@ "three"];  Error        Nsmutablearray *array2=[array mutablecopy];        [Array2 addobject:@ "three"];  Right        //Insert code        here ... NSLog (@ "Hello, world!");

V, shallow copy and deep copy

Shallow copy copies the object itself, the attributes in the object, the contained objects do not replicate

Deep copy copy all, including object properties and other objects

The foundation framework supports replicated classes, which by default are shallow copy

        Nsmutablearray *array=[[nsmutablearray alloc] init];        for (int i=0;i<3;i++)        {            nsobject *obj=[[nsobject alloc] init];            [Array addobject:obj];            [obj release];        }        For (NSObject *obj1 in array)        {            NSLog (@ "address is%p, reference count is%ld", obj1,obj1.retaincount);        }        Nsmutablearray *array2=[array copy];        For (NSObject *obj2 in array2)        {            NSLog (@ "address is%p, reference count is%ld", obj2,obj2.retaincount);        }

2013-09-30 17:28:01.492 fdas[681:303] address is 0x1001081f0, reference count is 12013-09-30 17:28:01.506 fdas[681:303] address is 0x100108230, The reference count is 12013-09-30 17:28:01.506 fdas[681:303] address is 0x100108240, reference count is 12013-09-30 17:28:01.507 fdas[681:303] address is 0X1001081F0, reference count is 22013-09-30 17:28:01.507 fdas[681:303] address is 0x100108230, reference count is 22013-09-30 17:28:01.507 FDAS[681:303] The address is 0x100108240 and the reference count is 2

V. Custom copies of objects

object has replication characteristics, it must implement the Nscopying,nsmutablecopying protocol, implement the Copywithzone method and Mutablecopywithzone method of the protocol.

The difference between a deep copy and a shallow copy is the realization of the Copywithzone method,

The shallow copy code is as follows:

#import <Foundation/Foundation.h> @interface person:nsobject<nscopying> @property (nonatomic,retain) NSString *name, @property (nonatomic,retain) nsstring *age; @end

#import "Person.h" @implementation person-(ID) Copywithzone: (Nszone *) zone{   //Implement custom light copy person    *person=[[self Class] allocwithzone:zone];    Person.age=_age;    Person.name=_name;    return person;} @end

The main function is:
    @autoreleasepool {person                *person=[[person alloc] init];        [Email protected] "Andy";        [email protected] ";                Person *person2=[person copy];        NSLog (@ "person address is%p,person2 address is%p", person.name,person2.name);    }
The output is:

2013-09-30 17:48:41.007 fdas[732:303] person address is 0x1000022c8,person2 address is 0X1000022C8

The deep copy code is as follows:

-(ID) Copywithzone: (Nszone *) zone{   //implementation of custom light copy person    *person=[[self class] allocwithzone:zone];    Person.age=[_age copy];    Person.name=[_age copy];    return person;}

Results:

2013-09-30 17:55:13.603 fdas[742:303] person address is 0x1000022c8,person2 address is 0x1000022e8

        Nsarray *arr=[nsarray arraywithobjects:@ "One", @ "one", nil];        Nsarray *arr2=[arr copy];        NSLog (@ "The dress of arr is%p the dress of arr2 is%p", ARR,ARR2);        NSLog (@ "The Retaincount is%ld", arr.retaincount);

The result of the execution is:

2013-09-30 18:01:01.394 fdas[787:303] The dress of arr is 0x100108320 the dress of arr2 are 0x100108320

2013-09-30 18:01:01.396 fdas[787:303] The Retaincount is 2


The result is the same because the copy method is optimized for the immutable copy object, which is equivalent to retain, so Retaincount becomes 2.

Equivalent to the Copywithzone method: return [self retain];

Relationship between the six, copy, Mutablecopy and retain

In a foundation object, when copy is an immutable object, the effect is equivalent to retain

When using mutablecopy, replicas are mutable regardless of whether the source object is mutable, and the true copy is implemented

When we use Copy a mutable object, the replica object is immutable.


About deep copies and shallow copies:

First, shallow copy:

    Car *car=[[[self class] allocwithzone:zone] init];    Car.engine=_engine;    Car.name=_name;    Car.weight=_weight;    return car;

Test code:

Car *car = [[Car alloc] init];        Engine *engine = [[Engine alloc] init];        Car.engine = engine;        [Engine release];        NSLog (@ "engine Retaincount is%lu", [engine retaincount]);        Car.name = @ "Audi";        Car.weight = @1000;        Car *car2=[car copy];       NSLog (@ "Car2 Retaincount is%lu", [Car2 Retaincount]);        NSLog (@ "Car%@,car2:%@", car.engine,car2.engine);

Output Result:

Car <Engine:0x100109000>,car2:<Engine:0x100109000>

You can see that shallow copy just duplicates the pointer and does not create a new memory space

Second, deep copy:

-(ID) Copywithzone: (Nszone *) zone {    /*** shallow copy **/    Car *car=[[[self class] allocwithzone:zone] init];    Engine *enginecopy=[[_engine copy] autorelease];    car.engine=enginecopy;        NSString *namecopy=[[_name copy] autorelease];    car.name=namecopy;        NSNumber *weightcopy=[[_weight copy] autorelease];    car.weight=weightcopy;    return car;}

Test code:

Car *car = [[Car alloc] init];        Engine *engine = [[Engine alloc] init];        Car.engine = engine;        [Engine release];        NSLog (@ "engine Retaincount is%lu", [engine retaincount]);        Car.name = @ "Audi";        Car.weight = @1000;        Car *car2=[car copy];       NSLog (@ "Car2 Retaincount is%lu", [Car2 Retaincount]);        NSLog (@ "Car%@,car2:%@", car.engine,car2.engine);

Results:

Car <Engine:0x100107ea0>,car2:<Engine:0x100108b70>

Opens up new space, zone represents a piece of memory space

Car *car=[[[self class] allocwithzone:zone] init];


Note that the code above uses "self class" instead of car, because if you use car, the child class of car will have a memory problem when calling this method to implement the copy protocol.

In addition, when a subclass inherits the parent class, he inherits all the attributes of the parent class, including the protocol to be implemented

Third, nsfoundation, when we copy an immutable object, the default copy is a shallow copy, the equivalent of retain

        Nsarray *array =[nsarray arraywithobjects:@ "One", @ "one", @ "three", nil];        Nsarray *array1 = [array copy];        NSLog (@ "%p", array);        NSLog (@ "%p", array1);        NSLog (@ "The Retaincount is%lu", [array retaincount]);

Output Result:

COPYDEMO1[673:303] 0x10010a5d0

2013-12-28 20:01:10.969 copydemo1[673:303] 0x10010a5d0

2013-12-28 20:01:10.969 copydemo1[673:303] The Retaincount is 2

Note that Retaincount will increase

When using Mutablecopy, a deep copy is implemented regardless of whether the object is mutable or not.

        Nsarray *array =[nsarray arraywithobjects:@ "One", @ "one", @ "three", nil];        Nsmutablearray *array1 = [array mutablecopy];        NSLog (@ "%p", array);        NSLog (@ "%p", array1);        NSLog (@ "The Retaincount is%lu", [array retaincount]);

Results:

COPYDEMO1[695:303] 0x10010a5d0

2013-12-28 20:07:08.570 copydemo1[695:303] 0x10010b260

2013-12-28 20:07:08.570 copydemo1[695:303] The Retaincount is 1

The retain corresponds to two objects pointing to the same pointer.

        Nsmutablearray *array1 = [[Nsmutablearray alloc] initwithobjects:@ "One", @ "one", @ "three", @ "Foure", nil];        Nsmutablearray *array2 = [array1 retain];        [Array2 Removelastobject];        NSLog (@ "%@", array1);        NSLog (@ "The Retaincount is%ld", array2.retaincount);
Results:

2013-12-28 20:13:02.915 copydemo1[736:303] (one    , both    ,    three) 2013-12-28 20:13:02.917 copydemo1[736:303] The Retaincount is 2







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.