Objective-c -- Analysis of attributes and member variables

Source: Internet
Author: User

As mentioned in the previous article, member variables are declared in a large pair of arc, while attributes are first declared by @ property and then synthesized by @ synthesize. Attribute is a new mechanism of OC language. In actual use, there are still many differences between the two.

Class member variables, through the permission modifier @ protected, @ private, @ public to change its access permission, and the attribute is belongs to the class object, by generating an object of this class, we can obtain its access permissions.

Firstclass. h

<Span style = "font-size: 12px;" >#import <Foundation/Foundation. h> @ interface firstclass: nsobject {// member variable of the class. The default access permission is protect int M. </span>
<Span style = "font-size: 12px;" >}// + class method + (void) classfun; //-indicates the instance method-(ID) initwithname :( nsstring *) aname andnum :( INT) N;-(void) print; @ property (nonatomic, strong) nsstring * Name; @ property (nonatomic, assign) int count; @ end </span> <span style = "font-size: 14px;"> </span>

In the header file, declare the (nsstring *) Name and (INT) Count variables. When @ property declares the variable, it will automatically generate the instance variable _ name starting with the following line, and _ count. and generate their setter and getter methods. When the @ synthesize merging attribute is omitted in the firstclass. m implementation file, the compiler automatically generates two instance variables: _ name and _ count.

<Span style = "font-size: 14px;"> // self. the attribute of the name access object. The _ name is the name of the instance and can be directly used, that is, self. name = _ name; </span>

Of course, this does not mean that @ synthesize does not work. @ synthesize variable = xxx. At this time, the actual operation object of self. variable is XXX rather than _ variable;

In the implementation file

@synthesize name;

The compiler automatically generates the instance variable of name and its corresponding setter and getter methods. Note that the generated instance variable is name rather than _ name.

The difference between "." and "->" in oC

Operations on objects in OC are implemented through pointer calls, and all member variables can be obtained through "->.

The "." operator is not available in the previous OC version. To adapt to the usage habits of Java, C ++, and other programmers, it is a new feature. "." In OC is actually a method call.

</PRE> </P> <P style = "margin-top: 0px; margin-bottom: 0px; font-family: Menlo; "> <PRE name =" code "class =" objc "> self. name = aname; <span style = "font-family: Menlo;"> //. setting the syntax to the left of the equal sign is equivalent to calling the setter method </span>
Nslog (@ "% @", self. Name); // put the. syntax to the right of the equal sign is equivalent to calling the getter Method

Below is the code for testing the above example

Firstclass. h

//// Firstclass. h // class property method member variable /// created by student on 14-9-17. // copyright (c) 2014 codebat. all rights reserved. // # import <Foundation/Foundation. h> @ interface firstclass: nsobject {// member variable of the class. The default access permission is protect @ public int M ;}// + class method + (void) classfun; //-indicates the instance method-(ID) initwithname :( nsstring *) aname andnum :( INT) N;-(void) print; @ property (nonatomic, strong) nsstring * Name; @ property (nonatomic, assign) int count; @ end

Firstclass. m

//// Firstclass. M // member variable of the class property method /// created by student on 14-9-17. // copyright (c) 2014 codebat. all rights reserved. // # import "firstclass. H "@ implementation firstclass @ synthesize name; // only name instances are generated at this time. _ name is not available @ synthesize COUNT = _ count; // specify the instance variable as _ count, this statement is omitted with the same effect + (void) classfun {nslog (@ "class method");}-(ID) initwithname :( nsstring *) aname andnum :( INT) m {If (Self = [Super init]) {self. name = aname; _ COUNT = m;} return self;}-(void) print {nslog (@ "% @ % d", self. name, self. count);} @ end
Main. m

//// Main. M // member variable of the class property method /// created by student on 14-9-17. // copyright (c) 2014 codebat. all rights reserved. // # import <Foundation/Foundation. h> # import "firstclass. H "int main (INT argc, const char * argv []) {@ autoreleasepool {firstclass * class = [[firstclass alloc] initwithname: @" doubi "andnum: 21]; class-> M = 20; Class. count = 22; [email protected] "SDS"; [class print];} return 0 ;}





Objective-c -- Analysis of attributes and member 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.