Objective-C static variable usage Summary

Source: Internet
Author: User

In Java, we often use Singleton mode. These design modes are also commonly used in iOS development. Recently, we are also considering using Singleton mode in iOS development.

In objective-C. the M file defines static variables to indicate global variables (similar to class variables in Java, but in objective-C, static variables are only initialized during compilation. For static variables, whether defined in the method body or outside the method body, the scope is the same.

In our frequently-used uitableviewcontroller, when defining uitablecellview, templates often use the followingCode

Help
1234567891011 -(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath { Static nsstring * cellidentifier = @ "cell "; Uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: cellidentifier forindexpath: indexpath]; Return cell; }

The static variable is defined above. This variable will be initialized and assigned a value during compilation, that is, the value of this variable is either nil or can be determined during compilation, generally, you can only use nsstring or the basic type, and this variable can only be accessed in cellforrowatindexpath. This variable is the same as the static attribute in Java, however, the static attribute of Java can be accessed in any Java class. Static variables defined in the method body can only be accessed in the corresponding access, but the variables are indeed class variables. This is the same as the static variable attribute in C.

Help
1234567891011 Void counter { Static int COUNT = 0; Count ++; } Counter (); Counter ();

After the preceding code is executed, the value of count for the first time is 1, and the value of count for the second call is 2.

Static variables can also be defined in the. m method, so that all methods can access this variable. However, there is no way to access the staticvar variable outside the class, that is, you cannot use xxxclass. staticvar to access the staticvar variable. Static variables are private.

If. the M file and the method body define static variables of the same name. The instance variables in the method body do not conflict with the Global static variables, the static variables accessed within the method body are different from the Global static variables.

Help
123456789101112131415161718192021 @ implementation ifoundappdelegate static nsstring * staticstr = @" test "; -(bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions { static nsstring * staticstr = @ "Test2 "; nslog (@ "The staticstr is % @ -- % d", staticstr, [staticstr hash]); } -(void) applicationwillresignactive :( uiapplication *) application { nslog (@ "The staticstr is % @ -- % d ", staticstr, [staticstr hash]); }

the above two static variables are two different variables. In the didfinishlaunchingwitexceptions method, the staticstr variable defined inside the method body is accessed, and in the applicationwillresignactive method body, the global staticstr variable for access. You can use the log to print its hash to confirm whether the two variables are the same.

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.