The difference between the "IOS" @interface and the @property way of declaring variables

Source: Internet
Author: User

In iOS programming, many people do not understand the difference between instance variables and attributes, I based on my experience, this here with you to share the difference, below I write a simple example:

. h

@interface Mycontroller:uiviewcontroller

{

UILabel *_mylabel;//Here is the instance variable declaration

}

@property (nonatomic, retain) uilabel*mylabel;//here is your attribute declaration

@end

. m

@implementation Mycontroller

@synthesize MyLabel = _mylabel;

...

@end

I believe everyone has seen such a program, but we generally write the program is the following:

. h

@interface Mycontroller:uiviewcontroller

{

UILabel *mylabel;//Here is the instance variable declaration

}

@property (nonatomic, retain) uilabel*mylabel;//here is your attribute declaration

@end

. m

@implementation Mycontroller

@synthesize MyLabel;

...

@end

In fact, the instance variables and attributes in the cocoa should be separate, and the following we usually write this code because the strength variable and the property is the same name, so do not use the equal sign to assign the instance variable to the attribute, the instance variable is generally used in a class inside, And when we use it, we create another class object in other classes, like we want to change the MyLabel Text property of the class we created above, what are we using? To clarify this problem, using the top of our code is the best, now we assume that we have created an object of Mycontroller, called Mycontroller, to change the text of MyLabel should be as follows:

MyController.myLabel.text [email protected] "This is changed after the content";

See Clearly, here is MyLabel (attribute), not _mylabel (instance variable), in Mycontroller this class "self". Can only attribute, instance variables are assign type, you need to directly alloc memory assignment to it, also in the last dealloc inside the release is also the instance variable rather than the attribute, the property is actually in the external or internal call time use instance variable of a way (personal understanding) , while the real memory is the instance variable.

used a section of OC; you'll find 2 ways to define variables

1. In the parentheses of @interface: nsobject{}, of course nsobject refers to a parent class, which can be other.

The form is as follows:

@interface Gcturnbasedmatchhelper:nsobject {
BOOL gamecenteravailable;
BOOL userauthenticated;
}

2. Another is to define a variable directly after the @interface: nsobject{} parenthesis, using @property.

@property (Assign, readonly) BOOL gamecenteravailable;

You'll find that once you've defined the variables in the @interface, it's common to repeat the same variables in the @property.

The result may be this:

@interface Gcturnbasedmatchhelper:nsobject {
BOOL gamecenteravailable;
BOOL userauthenticated;
}
@property (Assign, readonly) BOOL gamecenteravailable;

And you can define variables individually in @interface instead of @property definitions, or you can define them with @property instead of @interface, and of course @property to define them, usually in. m files with @ Synthsize to synthesize the corresponding Setter,getter method. Otherwise you will get a warning. Of course @synthsize is optional, but Apple recommended, no consequences, I have not tried, interested in children's shoes can try.

What is the difference between these two ways?

1. If you define variables only in @interface, the variables you define can only be accessed in the current class, not in other classes, and variables declared with @property may be accessed externally.

2. Using @property to declare variables, you can use the "self. Variable name" method to read and write variables. And the way to use @interface is not possible.

Here is a link: http://stackoverflow.com/questions/9702258/ Difference-between-properties-and-variables-in-ios-header-file inside said: I English dish, simple turn over:

Defining a variable in parentheses simply declares an instance variable (the member variable that the instance variable should refer to). Bo Main note: Foreigners to variable and instance variable have different understanding. So the following is a vague word Ivar.

Declaring (and @synthsize) a property generates getter and setter methods for member variables, and it is important to use setter and getter for memory management in OC according to the standard in parentheses. (for example, when a value is assigned to the variable, the object is allocated through the setter function, modifies the counter, and finally releases it). At a higher level, this approach also facilitates encapsulation and reduces unnecessary code.

It is common to define a variable in @interface brackets and repeat it with @property, which is not actually necessary. It is enough to use @property and @synthszie, because a variable is created when a read-write method of this property is synthesized with @synthsize.

The key is the following red font!!!!! Or look at the 3G grammar knowledge in the second half of the lecture.

Currently the recommended method for Apple (in the template) is this:

First define a property in the header file

@property int gamecenter;

Then synthsize and declare in the implementation file:

@synthesize gamecenter = __ Gamecenter;

The last line synthsize the Gamecenter property and explains that whatever value is assigned to this property is stored in the _gamecenter variable. Again, this is not necessary, but after this is written, you can reduce the input of variable names that have been explicitly named.

The last sentence of the meaning of you is reducing the locations where you had to type the name of the Ivar while still explicitly naming it. It's not good to flip.

According to the 2nd section of the Millennium Grammar course, this can make the @synthsize when the internal getter method exhibition into

-(int) Gamecenter {
return _gamecenter;
}

and directly write @synthsize gamecenter;

The setter function is expanded internally into a

-(int) Gamecenter {
return gamecenter;
}

Note: The function name and variable name are the same. In Stanford's course, Professor White Beard also vaguely said that the same name could bring a bug, specific what bug he did not say, I have not seen, so it is better to develop such a habit of writing. Getter functions in other languages typically add a get to a variable, but OC does not, and may be to distinguish it from other languages, which is the characteristic of OC, which brings about such trouble.

The difference between the "IOS" @interface and the @property way of declaring variables

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.