OBJECTIVE-C Basic syntax: The difference between an instance variable and a member variable

Source: Internet
Author: User


In Objective-c, defining a class requires two parts: the first is the interface (interface), and the second is the implementation (implementation). interface to the interface file, and the implementation corresponds to the implementation file. The interface file contains the declaration of the class, the member variable (member variable), and the method. interface files are usually. h



An implementation file is usually a. m file. The method that is declared in the interface needs to be implemented by x code in the. m file.




when viewing Apple's official documentation today (see below). Https://developer.apple.com/library/mac/referencelibrary/GettingStarted/Learning_Objective-C_A_Primer /index.html



A new noun was found: the member variable (member variable). It is novel because it is called an instance variable (instance variable). Remember, when first contact objective-c, see instance variable this noun, feel very novel. We also try to compare the instance variables with the member variables and study their differences. From this diagram below, it can be concluded that the instance variables I saw earlier, essentially, are member variables. For iOS beginners, a novel noun can easily make people think too much.



If you have been in contact with C + + before, the member variable (member Varialbe) is certainly not unfamiliar. Today, you see this familiar term, you will feel objective-c not as difficult as you think.



At this point, ears rang the slogan: The original formula, or the familiar taste. HA:)


in the figure "Member Variable declarations", literally means: "Member variable Declaration". It appears that the variable declared in {} is a member variable. So what does an instance variable mean? since OC is expressed in English, it may be useful to look up the original meaning in English.

Class: Class (Description/Template ForAnObject)
Example: Instance (manifestation of a class)
News: Message (Sent toObjectTo make It act)
Method: Method (Code invokedByAMessage Instance variable ( Object-specific storage)
superclass/subclass ( inheritance)
protocol:   protocol (non-class-specific methods)

From the English explanation given, it can be seen that the instance (Instance) is for class. 

Strictly speaking, the int count in; is a member variable. and NSString * name; is an instance variable. Should ID data belong to a member variable or an instance variable? because the ID is an OC-specific type. In essence, theID is equivalent to (void *). 

 so ID data should belong to the instance variable.  


OBJECTIVE-C Basic syntax: The difference between an instance variable and a member variable


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.