IOS 21st-day notes (ARC memory management-Copy-proxy), ios-copy-

Source: Internet
Author: User

IOS 21st-day notes (ARC memory management-Copy-proxy), ios-copy-

Knowledge points of IOS Learning (oc language)

 

1. ARC memory management in OC

1) Principle of releasing the object memory in ARC: Check whether the object is strongly referenced to it.

2) strong: strong references. By default, all references are strongly referenced.

3) weak: weak quote _ weak

4) in the ARC environment: no memory-related code can be used. If you want to use MRC memory to manage code in the ARC environment, for example:

[Super delloc] select the project and find the. m file to be converted in the Compile Sources item under the Build Phases menu,

Double-click to write this line code:-fno-objc-arc.

5) Convert MRC code To ARC Code operation: You can select To Objective-c arc… from Convert under the Edit menu in the upper left corner of Xcode...

Item.

6) if the two pointers of two objects reference each other, the memory of the object cannot be released. Solution: one end uses strong,

One end uses weak. If it is the basic type in peroperty, use assign

7) ARC operation instance code:

1 // main. code 2 Person * p1 = [[Person alloc] init] in m; 3 // The previous object does not point to a strong reference, the memory will be released 4 p1 = [[Person alloc] init]; 5 NSLog (@ "*********"); 6 // incorrect syntax, indicates that there is a weak reference pointing to the object. After this statement is executed, the object will be released 7 _ weak Person * p2 = [[Person alloc] init]; 8 NSLog (@ "++ ========"); 9 10 // Person. code 11 # import <Foundation/Foundation. h> 12 @ class Card; 13 @ interface Person: NSObject14 @ property (nonatomic, strong) Card * card; 15 @ property (nonatomic, strong) NSString * name; 16 @ property (nonatomic, assign) int age; 17 @ end18 19 // Card. code 20 # import <Foundation/Foundation. h> 21 # import "Person. h "22 @ interface Card: NSObject23 @ property (nonatomic, weak) Person * person; 24 @ end

 

II. Introduction to copy and mutableCopy

1) send a copy message to the string to obtain an immutable string (whether it is a message sent to a mutable or immutable string)

2) Send a mutableCopy message to the string to obtain a variable string (whether it is a message sent to a variable or an immutable string)

3) copy operation instance of String object:

1 NSString * str1 = @ "Hello"; 2 NSString * str2 = str1; 3 // send a copy message to an immutable string to get a new immutable string 4 NSString * str3 = [str1 copy]; 5 // [str3 appendString: @ "shanghai"]; 6 // a new variable string 7 NSMutableString * str4 = [str1 mutableCopy] will be generated after the mutableCopy message is sent to the immutable string; 8 [str4 appendString: @ "beijing"]; 9 NSLog (@ "str4: % @", str4); 10 NSMutableString * mStr1 = [NSMutableString stringWithString: str1]; 11 // a new immutable string 12 NSString * mStr2 = [mStr1 copy] will be generated when a copy message is sent to a variable string; 13 // [mStr2 appendString: @ "aaa"]; 14 15 // send the mutableCopy message to the variable string to get a new variable String object 16 NSMutableString * mStr3 = [mStr1 mutableCopy]; 17 [mStr3 appendString: @ "abc"]; 18 NSLog (@ "mStr3 = % @", mStr3 );

 

4) Implement the copy instance code for the custom object:

1. Define the code in the javascar class. h file as follows:

1 #import <Foundation/Foundation.h> 2 @interface QFCar : NSObject<NSCopying>3 @property(nonatomic,copy)NSString *name;4 @property(nonatomic,assign)int year;5 -(void)print;6 @end

 

2. The code in the. m file is as follows:

1 # import "your car. h "2 @ implementation mongocar 3 // This Protocol method is executed when the copy method is called 4-(id) copyWithZone :( NSZone *) zone 5 {6 // your car * car = [[your car allocWithZone: zone] init]; 7 // You can inherit from the quilt class, the Copied object is the subclass object 8 // [self class] to obtain the current class (object) 9. car * car = [[[self class] allocWithZone: zone] init]; 10 car. name = self. name; 11 car. year = self. year; 12 return car; 13} 14 15-(void) print16 {17 18 NSLog (@ "name is % @, year is % d", _ name, _ year ); 19} 20 @ end

 

3. Implementation code in the main file:

1 primary car * mCar = [[primary car alloc] init]; 2 NSString * n1 = @ "BMW X6"; 3 NSMutableString * n = [[NSMutableString alloc] initWithString: n1]; 4 mCar. name = n; 5 mCar. year = 2015; 6 [mCar print]; 7 [n appendString: @ "kingkong"]; 8 [mCar print];

 

5) Introduction to shortest copy and deep copy

1. These two copies are for numbers or dictionary sets.

2. The shortest copy only copies the array object. The two array objects store the address of the same element. The element objects in the array are not copied.

3. Deep copy not only copies array objects, but also copies a new element object in the array.

6) Deep copy and shallow copy instance code

1 NSMutableArray * carList = [[NSMutableArray alloc] init]; 2 for (int I = 0; I <5; I ++) {3 primary car * car = [[primary car alloc] init]; // custom Class Object 4 car. name = [NSString stringWithFormat: @ "bmw x % d", I + 1]; 5 car. year = 2011 + I; 6 [carList addObject: car]; 7} 8 9 // use copy (mutableCopy) to implement a shortest copy of an array (only copy an array object, the two array objects store the addresses of the same elements, and the element objects in the array are not copied.) 10 // NSMutableArray * array1 = [carList mutableCopy]; 11 12 // shallow copy 13 NSMutableArray * array1 = [[NSMutableArray alloc] initWithArray: carList]; 14 15 // implements deep copy of arrays (not only copying array objects, the element objects in the array will also be copied to a new one.) 16 NSMutableArray * array2 = [[NSMutableArray alloc] initWithArray: carList copyItems: YES]; 17 [[array2 lastObject] setName: @ "shanghai"]; 18 for (your car * car in array2) {19 [car print]; 20} 21 NSLog (@ "************"); 22 for (your car * car in carList) {23 [car print]; 24}

 

Iii. Introduction to agents in OC

1) proxy is a process in which you declare that the method is not implemented by other classes or objects. The proxy aims to reduce code coupling.

2) generally, a protocol (protocol) proxy must comply with the protocol to implement the protocol.

3) proxy instance code

1. Create two class Object HeadMaster (principal class) Teacher (principal class); principal class proxy principal class examine,

Meeting and travle Methods

2. The code implementation in the HeadMaster. h file is as follows:

1 #import <Foundation/Foundation.h>  2 @protocol MasterDelegate<NSObject> 3 -(void)examine; 5 -(void)meeting; 7 -(void)travle; 9 @end11 @interface HeadMaster : NSObject13 @property(nonatomic,strong)id<MasterDelegate> delegate;15 -(void)masterExamine;17 -(void)masterMeeting;19 -(void)masterTravle;21 @end

3. The code implementation in the HeadMaster. m file is as follows:

 1 #import "HeadMaster.h"  2 @implementation HeadMaster 3 -(void)masterExamine 4 { 5      if([_delegate conformsToProtocol:@protocol(MasterDelegate)]){ 6         if([_delegate respondsToSelector:@selector(examine)]){ 7             [_delegate examine]; 8         } 9     }10 } 12 -(void)masterMeeting13 {14     if([_delegate conformsToProtocol:@protocol(MasterDelegate)]){15         if([_delegate respondsToSelector:@selector(meeting)]){16             [_delegate meeting];17         }18     }19 }20 21 -(void)masterTravle22 {23     if([_delegate conformsToProtocol:@protocol(MasterDelegate)]){24         if([_delegate respondsToSelector:@selector(travle)]){25             [_delegate travle];26         }27     }28 }29 @end

 

4. The code implementation in the Teacher. h file is as follows:

1 #import <Foundation/Foundation.h>2 #import "HeadMaster.h"3 @interface Teacher : NSObject<MasterDelegate>4 @end

 

5. The code implementation in the Teacher. m file is as follows:

 1 #import "Teacher.h" 2 @implementation Teacher 3 -(void)examine 4 { 5     NSLog(@"teacher examine"); 6 } 7  8 -(void)meeting 9 {10     NSLog(@"teacher meeting");11 }12 13 -(void)travle14 {15     NSLog(@"teacher travle");16 }17 @end

 

6. The (Implementation call) code in the main. m file is as follows:

 1 #import <Foundation/Foundation.h> 2 #import "Teacher.h" 3 int main(int argc, const char * argv[]) { 4     @autoreleasepool { 5         HeadMaster *master=[[HeadMaster alloc]init]; 6         Teacher *xiaozhang=[[Teacher alloc]init]; 7         master.delegate=xiaozhang; 8         [master masterExamine]; 9         [master masterMeeting];10         [master masterTravle];11     }12     return 0;13 }

 

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.