Objective-CAboutSelf.The usage is the content to be described in this article. If you do not talk about it, you can directly access the topic. We often see this code in official documents:
- MyClass.h
-
- [/lang]
- @interface MyClass : NSObject {
- MyObject *myObject;
- }
- @property (nonatomic, retain) MyObject *myObject;
- @end
- MyClass.m
- @synthesize myObject;
- -(id)init{
- if(self = [super init]){
- MyObject * aMyObject = [[MyObject alloc] init];
- self.myObject = aMyObject;
- [aMyObject release];
- }
- return self;
- }
Some people ask why such a complicated assignment is required? Why addSelf.? Directly writeSelf.MyObject = [[MyObject alloc] init]; isn't it true? Sometimes it seems that it is normal to not add self? Now let's take a look at the memory management content:
First look at the indirect assignment:
1. Add self .:
- MyObject * aMyObject = [[MyObject alloc] init]; //aMyObject retainCount = 1;
- self.myObject = aMyObject; //myObject retainCount = 2;
- [aMyObject release];//myObject retainCount = 1;
2. Do not add self .:
- MyObject * aMyObject = [[MyObject alloc] init]; // aMyObject retainCount = 1;
- MyObject = aMyObject; // myObject retainCount = 1;
- [AMyObject release]; // the object has been released
Let's look at the directly assigned values:
3. Add self .:
- self
- .myObject
- = [[
- MyObject
- alloc] init]
- ;
- //myObject retainCount = 2;
4. Do not add self .:
- myObject = [[MyObject alloc] init]; //myObject retainCount = 1;
Is it a bit dizzy now, let's change the code first, a common official method:
- MyClass.h
-
- @interface MyClass : NSObject {
- MyObject * _myObject;
- }
- @property (nonatomic, retain) MyObject *myObject;
- @end
- MyClass.m
- @synthesize myObject = _myObject;
OK. Try again now. If you use self. _ myObject = aMyObject; or myObject = aMyObject; you will get an error. Why? This is related to the Obj-c access method. to put it simply, we all know that @ property (nonatomic, retain) MyObject * myObject; is used to set an access method for an attribute, but the method name and attribute name we use are the same at ordinary times, now you have written it into different names, and it will be clear. _ myObject is the property and myObject is the name of the access method.
Now we know that self is the access method for access attributes. How does the access method work? Self. myObject = [[MyObject alloc] init]; why is memory leakage?
I will not explain nonatomic much about it. It is not the focus of my talk, and I have not fully figured it out and won't mislead everyone. I only talk about assign, retain, copy.
The get method is:
- -(MyObject*)myObject{
- return _myObject;
- }
The Set method is:
- // assign
- -(void)setMyObject:(id)newValue{
- _myObject = newValue;
- }
- // retain
- -(void)setMyObject:(id)newValue{
- if (_myObject != newValue) {
- [_myObject release];
- _myObject = [newValue retain];
- }
- }
- // copy
- -(void)setMyObject:(id)newValue{
- if (_myObject != newValue) {
- [_myObject release];
- _myObject = [newValue copy];
- }
- }
In fact, there are other content in these methods, not just these. And these methods can be rewritten. For example, if you write
- -(MyObject*)myObject{
- return _myObject;
- }
Put it in your class. When you call self. myObject (do not place it on the left of the equal sign, that will call the get method), this method will be called.
Here, I would like to say that @ property sets an access method for you. It has nothing to do with your properties. You can write only one sentence.
- @property (readonly) NSString *name;
Implement in your class
- -(NSString*)name{
- NSLog(@"name");
- return @"MyClass";
- }
You can also useSelf.Name call.
Now let's look back at the four assignments we started.Self.In this case, only a general value is assigned. Assigning a pointer to another pointer does not affect the allocated memory. Therefore, do not [aMyObject release] in 2. this sentence is the same as 4. I will not talk about it here. let's look at 1 and 3,
When setMyObject: method is called, newValue is retaken. We must release the original newValue. Otherwise, the memory will be leaked. In 1, we have a aMyObject that can be released. In 3, we cannot release it. Therefore, in 3, we will add a retainCount. memory leakage.
After talking about this, I just want to let everyone know what is the call property itself and what is the call access method. how can we avoid Memory leakage? In the above example, it is called in your own class. If this class is called by another class, pay more attention to it,
By the way, if you want to access object attributes in other classes, instead of using the access method, you can use myClass-> myObject to access the object directly, however, you must first set myObject to @ public. but this is not officially promoted,
Code is relatively simple, I still sent out, high people can ignore the attachment: SelfExample.zip (18 K) http://www.cocoachina.com/bbs/job.php? Action = download & aid = 6639
Summary:Objective-CAboutSelf.I hope this article will help you!
Post address http://www.cocoachina.com/bbs/read.php? Tid-12850-fpage-11-page-1.html. Welcome to the discussion.