Detailed description of iOS development

Source: Internet
Author: User

Before many netizens to my translation of the use of the property in the tutorial is somewhat puzzled, do not know when to release, when to Self.xxx = nil, and objective-c memory management and COCOS2D memory management rules are not clear. This article mainly explains OBJC inside @property, it is what, it has what use, atomic,nonatomic,readonly,readwrite,assign,retain,copy,getter,setter these key words have what use And when to use them. As for OBJC memory management and COCOS2D Memory Management section, next, I will translate Ray's 3 tutorials, which is discussed in detail with you. Our main task today is to take care of @property.
A friend of C + + knows that when we define STRUCT/CLASS, if we set the Access qualifier (public,protected,private) to public, we can use the. Number to access its internal data members. Like what
InchTest.h
ClassTest
{
Public
int i;
float F;
};
In the main function, I can use this class in the following way: (Note that if you use this class in the main function, in addition to include the header file, the most important thing is to remember to change the MAIN.M to MAIN.MM, otherwise you will report some strange errors.) So, whenever we use C + +, if we report a strange mistake, we should remind ourselves whether to change the corresponding source file to. mm suffix. Other files that refer to this class are sometimes changed to. mm files)
In main.mm
   TestTest
   TEST.I = 1;
   TEST.F =2.4f;
NSLog (@ "test.i =%d, test.f =%f", test.i,? test.f);
But, in OBJC, can we do that? Take a look at the following code: (Create a new OBJC class, named BaseClass)
In BaseClass.h
@interface baseclass:nsobject{
@public
NSString *_name;
}
Next, we are inside the MAIN.MM:
BaseClass *base= [[BaseClass alloc] init];
Base.name? [Email protected] "set?base name";
NSLog (@ "base class ' s name =%@", base.name);
Do not wait for you to compile, Xcode4 immediately prompt error, please see:

Please pay attention to the error message "Property' Nam ' isn't found on object of type baseclass* ", meaning, BaseClass This class does not have a property named name. Even if we declare @public in the header file, we still cannot use the. Number to directly access its data members while using BaseClass. @public, @protected and @private only affect access to the classes that inherit it, and if you use @private to declare data members, you cannot use the private members of the parent class directly in the subclass, which is the same as C++,java.
Since there are errors, then we have to solve the idea, the compiler said no @property, well, we define the property, see the code:
In BaseClass.h
@interface baseclass:nsobject{
@public 
nsstring *_name;  
} 
@property (nonatomic,copy) nsstring *name; 
//in baseclass.m 
@synthesize name = _name;  
now, compile and run, OK, fine. Then you may ask @prperty is not to let ". "The number is legal?" As long as you have defined @property, you can use the. Number to access the data members of the class? First let's look at the following example:
@interface baseclass:nsobject{
@public
NSString *_name;
}
//@property (nonatomic,copy) NSString *name;
-(nsstring*) name;
-(void) SetName: (nsstring*) NewName;
I commented out the definition of @property, and also defined two functions, name and setname, see implementation file below:
//@synthesize? name = _name;
-(nsstring*) name{
return _name;
}
-(void) SetName: (NSString *) name{
if (_name! = name) {
[_name release];
_name = [name copy];
}
}

Now, you can compile and run again, and work just as well. Why Because the work I have just done is exactly the same as the previous statement @property. @prperty is nothing more than a directive to the compiler, which can compile and generate the appropriate getter and setter methods for you. Also, notice that the copy parameter is inside the Face property (nonatomic,copy) parenthesis? What it does is
_name = [name copy];
If you specify retain, or assign, then the corresponding code is:
Property (retain) nsstring* name;
_name = [name retain];
Property (assign) nsstring* name;
_name = name;
As you can see here, @property not just can generate getter and setter methods, it can also do memory management. But I'm not talking about it here. Now, @property probably did something, presumably everybody already knew. However, we programmers have a hurdle, is that they do not completely thoroughly understand things, the heart of use is not practical, especially myself. So, next, we'll dig deep into every detail of @property.
First, we look at the difference and usage between atomic and nonatomic, before we start by looking at the following code:
@property (nonatomic, retain) Uitextfield *username;??? 1
@property (nonatomic, retain,readwrite) Uitextfield *username;? 2
@property (Atomic, retain) Uitextfield *username;? 3
@property (retain) Uitextfield *username;? 4
@property (atomic,assign) int i;???????? 5
@property (atomic) int i;???????? 6
@property int i;?????????????? 7
Ask the reader to stop and think, what is the difference between them?
The above code 1 and 2 are equivalent, 3 and 4 are equivalent, and 5,6,7 is equivalent. This means that atomic is the default behavior, assign is the default behavior, and ReadWrite is the default behavior. However, if you write on @property (nontomic) nsstring *name, then a warning will be reported, such as:

Because it is a non-GC object, the default assign modifier is not possible. Then when to use assign, when to use retain and copy it? Recommended practice is nsstring with Copy,delegate with assign (and must use assign, do not ask why, just go with it, you will understand later), non-OBJC data types, such as int, Basic data types such as float are used with assign (default is assign), while other OBJC types, such as nsarray,nsdate retain.
Before I go on, I would like to add a few more questions, that is, if we ourselves define the setter method for some variables, but want the compiler to generate a getter method for us, is that OK? The answer is of course it can. If you implement the Setter/getter method in your. m file yourself, then the translator will not generate the corresponding getter/setter for you. Take a look at the following code:
Code One:
@interface baseclass:nsobject{
@public
NSString *_name;
}
@property (nonatomic,copy,readonly) nsstring *name;? The readonly is used here, and all the Geter methods are declared.
-(void) SetName: (nsstring*) NewName;
Code two:
@interface baseclass:nsobject{
@public
NSString *_name;
}
@property (nonatomic,copy,readonly) nsstring *name;?? Although ReadOnly is declared here, the Getter method is not generated because you define the getter method below yourself.
-(nsstring*) name;?? Is the Getter method only a name? Not necessarily, you open foundation.framework, find UIView.h, look inside the property will understand)
-(void) SetName: (nsstring*) NewName;
Code Three:
@interface baseclass:nsobject{
@public
NSString *_name;
}
@property (nonatomic,copy,readwrite) nsstring *name;? Here the compiler will generate getter and setter
Code four:
@interface baseclass:nsobject{
@public
NSString *_name;
}
@property (nonatomic,copy) nsstring *name;? Because ReadWrite is the default behavior, the same code three
The above four sections of code are equivalent, and next, take a look at the following four pieces of code:
Code One:
@synthesize name = _name;? In this sentence, the compiler discovers that you do not define any getter and setter, so you will generate getter and setter at the same time
Code two:
@synthesize name = _name;? Because you define name, which is the getter method, the compiler simply generates a setter method, which is the SetName method.
-(nsstring*) name{
NSLog (@ "name");
return _name;
}
Code Three:
@synthesize name = _name;?? Because you define the setter method here, the compiler will only generate getter methods for you.
-(void) SetName: (NSString *) name{
NSLog (@ "SetName");
if (_name! = name) {
[_name release];
_name = [name copy];
}
}
Code four:
@synthesize name = _name;? Here you define the getter and setter, this sentence is useless, you can comment out.
-(nsstring*) name{
NSLog (@ "name");
return _name;
}
-(void) SetName: (NSString *) name{
NSLog (@ "SetName");
if (_name! = name) {
[_name release];
_name = [name copy];
}
}
The above four pieces of code are also equivalent. See here, people on the role of property will have a further understanding of it. But you have to be careful, if you use the property, and you rewrite the setter/getter yourself, you need to understand clearly what you have done. Do not write the following code, although it is legal, but will mislead others:
BaseClass.h
@interface baseclass:nsobject{
@public
Nsarray *_names;
}
@property (nonatomic,assgin,readonly) Nsarray *names;? Notice this is assign.
-(void) Setnames: (nsarray*) names;
Baseclass.m
@implementation BaseClass
@synthesize names = _names;
-(nsarray*) names{
NSLog (@ "names");
return _names;
}
-(void) Setnames: (nsarray*) names{
NSLog (@ "Setnames");
if (_name! = name) {
[_name release];
_name = [name retain];? You retain, but you do not overwrite this method, then the compiler will generate Setnames method, which must be used assign
}
}
When someone uses @property to do memory management, there is a problem. To summarize, if you implement the getter and setter yourself, atomic/nonatomic/retain/assign/copy These are just suggestions for compiling, the compilation will first go to your code to find, If you define the appropriate getter and setter, then good, use yours. If not, the compiler will generate the appropriate getter and setter based on some of the rules you specify in Atomic/nonatomic/retain/assign/copy.
Well, say so much, go back to our point. The roles and differences of atomic and nonatomic: 
If you use @synthesize to get the compiler to generate code, atomic and nonatomic generate code that is not the same. If you use atomic, such as its name, it will ensure that each getter and setter operation will be performed correctly, without worrying about the other threads being set at the time of your get, it can be said that a certain degree of thread safety is ensured. However, I went online to check the information, just rely on atomic to ensure that thread safety is very naïve. To write thread-safe code, you also need to have synchronization and mutex mechanisms.  
and Nonatomic does not have a similar "thread safety" (I quote here refers to some degree of thread safety) guaranteed. Therefore, it is clear that nonatomic is faster than atomic. And that's why, basically all of the property we use is nonatomic.  
There may be readers who often see this code in the Dealloc function of my tutorial: self.xxx = nil; see here, Now you know what's the use of this writing? It is equivalent to [XXX release]; xxx = [nil retain];(---if your property (Nonatomic,retian) xxx, then this will be the case, if not, it will be.  
because nil can send any message to it without error. Why does the release have to be assigned a value of nil? When you use C, you have this coding habit.  
int* arr = new INT[10];??? Then delete arr when not in use, arr = NULL;? In the OBJC can use a sentence Self.arr = nil; it's done.

This article transferred from:http://www.spasvo.com/news/html/20141121145332.html

Detailed description of iOS development

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.