Everyone should know that @ represents the "objective-c" sign, proving that you are using the objective-C language, but it is a headache for property and synthesize, For the first iPhone development path.
Let me take it for myself. When I first learned objective-C, I didn't know what the @ property and @ synthesize functions. Why should I add these two things, what are these two things used for? The brain is confused. My mind is blank, and I will understand it later. After reading the book, I will be more clear. Finally, I have a deep heart.
For the sake of newbie, do not take the same path as I used to, and write out these two relationships. You are awake now.
(That's it. It's just a newbie. If you're Nb, Please bypass it .. Don't joke or attack me. I 'd like to say that I am a kind of kind, but I can come in and discuss it together ...)
First, paste two demos. Let's take a look:
Demo 1:
# Import <uikit/uikit. h>
@ Interface property1viewcontroller: uiviewcontroller {
Nsinteger number1;
Nsinteger number2;
}
-(Void) setnumber1 :( nsinteger) num;
-(Void) setnumber2 :( nsinteger) num;
-(Nsinteger) number1;
-(Nsinteger) number2;
@ End
# Import "property1viewcontroller. H"
@ Implementation property1viewcontroller
// Implement viewdidload to do additional setup after loading the view, typically from a nib.
-(Void) viewdidload {
[Self setnumber1: 1];
[Self setnumber2: 2];
Nslog (@ "number1: % d number1 + 2: % d", [self number1], number1 + 2 );
Nslog (@ "number2: % d number2 + 2: % d", [self number2], number2 + 2 );
[Super viewdidload];
}
// Setter method of the object
-(Void) setnumber1 :( nsinteger) num {
Number1 = num;
}
-(Void) setnumber2 :( nsinteger) num {
Number2 = num;
}
// Getter method of the object
-(Nsinteger) number1 {
Return number1;
}
-(Nsinteger) number2 {
Return number2;
}
-(Void) dealloc {
[Super dealloc];
}
@ End
DEMO 2: simplified version (property and synthesize)
# Import <uikit/uikit. h>
@ Interface propertyviewcontroller: uiviewcontroller {
Nsinteger number1;
Nsinteger number2;
}
@ Property nsinteger number1;
@ Property nsinteger number2;
@ End
# Import "propertyviewcontroller. H"
@ Implementation propertyviewcontroller
@ Synthesize number1, number2;
// Implement viewdidload to do additional setup after loading the view, typically from a nib.
-(Void) viewdidload {
[Self setnumber1: 1];
[Self setnumber2: 2];
Nslog (@ "number1: % d \ nnumber1 + 2: % d", [self number1], number1 + 2 );
Nslog (@ "number2: % d \ nnumber2 + 2: % d", [self number2], number2 + 2 );
[Super viewdidload];
}
-(Void) dealloc {
[Super dealloc];
}
@ End
The output values of these two demos are the same: See figure 1. In fact, the purpose of the two is to achieve the same, but the method is different, the program writing is good or bad, mainly to look at the code, use the most brief code to achieve your purpose, the premise is that the goal must be perfect. Pay attention to efficiency, of course, quality is undoubtedly the most important.
First, browse both the demos, look at the red section, and analyze their differences,
The red part in demo 1 is described as follows:
It is the setter and getter methods for manually generating objects.
The red part in DEMO 2 is described as follows:
@ Property nsinteger number1;
@ Property nsinteger number2;
@ Synthesize number1, number2;
The @ property feature is used to automatically generate the setter and getter methods.