Static member functions are used in xcode to replace global variables.

Source: Internet
Author: User
Objective C supports global variables in the same usage as C/C ++. However, when the following requirements are met: for example, variables defined in Class A can be accessed and modified without creating an instance of Class A in Class B. It is not appropriate to use global variables. In this case, we need to define a static variable in the implementation file of Class A, and then define a static member function (class method) for Class A to operate on the variable. In this way, you do not need to create a class A instance to access static variables in other classes. Although this static variable is not a static member variable of Class A, it achieves the same effect. The key to not defining global variables is the scope of variables. The scope of static variables is limited to a single file. Code// Example. h @ interface Example: nsobject {}-(ID) Init; + (INT) instancecount; @ end copy the code // example. M # import "example. H "static int count; @ implementation example-(ID) Init {self = [Super init]; If (nil! = Self) {count + = 1;} return self;} + (INT) instancecount {return count ;} @ end copy code in the above example, you can use [Example instancecount] to access the static variable count without creating an instance.

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.