Summary of the use of objective-c static variables

Source: Internet
Author: User

In Java, we often use singleton patterns, which are more commonly used in iOS development, and have recently been considered using singleton mode in iOS development.

In the Objective-c, It is necessary to define a static variable in the. m file to represent the global variable (similar to the class variable in Java, but in objective-c, the static variable is initialized only at compile time, for the static variable, whether it is defined in the method body Or outside of the method body, the scope is the same.

In the uitableviewcontroller we often use, when defining uitablecellview, templates often use the following code

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, which initializes the variable at compile time, meaning that the value of the variable is either nil, or the value can be determined at compile time, in general, only with nsstring or basic type. And this variable can only be accessed in Cellforrowatindexpath, this variable is the same as the static property inside Java, but the static property of Java is any access that can be inside the Java class, and the static variable defined in the method body can only be Access inside the corresponding access, but the variable is indeed a class variable. This is the same as the static variable property of the C language.

1234567891011 void counter{static int count = 0; count ++; } counter();counter();

After the above code execution completes, the value of the first count is 1, and the second call to count is 2

The static variable can also be defined in the. m method body, so that the variable can be accessed internally by all methods. But outside of the class there is no way to access the Staticvar variable in the Xxxclass.staticvar way. The equivalent of a static variable is private.

If the. m file and the method body define a static variable of the same name, then the instance variable inside the method body does not conflict with the global static variable, and the static variable that is accessed inside the method body is different from the global static variable.

123456789101112131415161718192021 @implementation ifoundappdelegate  static nsstring * staticstr  = @ "Test";  -(BOOL) Application: (UIApplication *) application Didfinishlaunchingwithoptions: (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, within the Didfinishlaunchingwithoptions method, Access is the STATICSTR variable defined inside the method body, Inside the Applicationwillresignactive method body, access the globally defined STATICSTR variable. You can confirm whether the two variables are the same by printing their hash in the log.

Summary of the use of objective-c static 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.