rules for memory management :
1.alloc created must be freed, convenience constructor created do not release
2. Objects added to the container are executed once retain operation, reference count plus 1
3. The container removes the object and sends a release message to the object, reducing the object's reference count by 1
4. When the container is released, a release message is sent to all objects in the container
When memory management is performed, it is not an error to follow a release once each increment is followed. Here is an example:
Define a Hero class and add the following properties and initialization methods:
1 @property (nonatomic, copy) NSString *name; 2 @property (nonatomic, assign) Nsinteger speed; 3 @property (nonatomic, retain) NSString *ability; 4 5 -(Instancetype) Initwithname: (NSString *) name speed: (nsinteger) Speed Ability: (NSString *) ability; 6 + (Hero *) Herowithname: (NSString *) name speed: (nsinteger) Speed Ability: (NSString *) ability;
Be sure to set the properties in the. m file:
1 @synthesize name = _name; 2 @synthesize speed = _speed; 3 @synthesize ability = _ability;
Then implement the initialization method and destroy method:
1- (void) Dealloc2 {3 //because speed is not an object type, you do not need to free memory4 [_name release];5 [_ability release];6 [Super Dealloc]; 7 }8 9-(Instancetype) Initwithname: (NSString *) name speed: (nsinteger) Speed Ability: (NSString *) AbilityTen { OneSelf =[Super init]; A if(self) - { -_name =name; the_speed =Speed ; -_ability =ability; - } - returnSelf ; + } - ++ (Hero *) Herowithname: (NSString *) name speed: (nsinteger) Speed Ability: (NSString *) Ability A { at return[[[Hero alloc] initwithname:name speed:speed ability:ability] autorelease]; -}
As well as setter and getter methods:
1- (void) SetName: (NSString *) name2 {3 if(_name! =name)4 {5 [_name release];6_name =[name copy];7 }8 } 9-(NSString *) nameTen { One return[[_name retain] autorelease]; A } - -- (void) Setability: (NSString *) Ability the { - if(_ability! =ability) - { - [_ability release]; +_ability =[ability retain]; - } + } A-(NSString *) Ability at { - return[[Ability retain] autorelease]; -}
KVC
Key value Coding, which is a method of indirectly accessing instance variables.
KVC provides a mechanism to access an object instance variable using a string (key) instead of an accessor method.
Common methods:
-(ID) Valueforkey: (NSString *) key; Gets the value of the instance variable by key (instance variable name).
-(void) SetValue: (ID) value Forkey: (NSString *) key; Assigns a value to an instance variable by key (instance variable name).
-(ID) Valueforkeypath: (NSString *) KeyPath; Gets the value of the instance variable by KeyPath.
-(void) SetValue: (ID) value Forkeypath: (NSString *) KeyPath; Assigns a value to an instance variable by KeyPath.
-(void) Setvaluesforkeyswithdictionary: (nsdictionary *) keyedvalues; assigns the key value of a Dictionary object to each instance variable of the object.
Attention! When using KVC, if the key value and the property name are different, it crashes. Before you resolve the situation, take a look at the internal implementation process of KVC:
A.Setvalue:forkey:(if the given key is name)
1. Go to the class to find out if there is a method called SetName:, some words assignment, no words to perform the second step
2. Go to the class to find if there is an instance variable called _name, some words assignment, no words to perform the third step
3. Go to the class to find if there is an instance variable called name, some words assignment, no words to perform the fourth step
4. Find out whether the current class implements the Setvalue:forundefinedkey: method, if any, walk the method internal implementation, if not yet, will throw an exception, causing a crash
B.Valueforkey:(assuming the given key is name)
1. Go to the class inside to find out if there is a method called GetName:, some words to value, no words to perform the second step
2. Go to the class to find if there is an instance variable called _name, some words to value, no words to perform the third step
3. Go to the class to find if there is an instance variable called name, some words to value, no words to perform the fourth step
4. Find out whether the current class implements the Valueforundefinedkey: method, if any, walk the method internal implementation, if not yet, will throw an exception, causing a crash
So in order to prevent the crash, you only need to re-implement the following setvalue:forundefinedkey: and valueforundefinedkey: These two methods are:
1 -(void) SetValue: (ID) value forundefinedkey: (NSString *) key2{ 34}56 -(ID) Valueforundefinedkey: (NSString *) Key7{8 return nil; 9 }
Setvalue:forundefinedkey: The method can be filled with related operations
"Objective-c Study record" 29th Day