How to use static variables in Objective-C

Source: Internet
Author: User

Objective-CMediumStatic variablesThe usage is described in this article,Objective-CGlobal supportVariableThere are two main implementation methods: the first one is the same as in C/C ++, and the "extern" keyword is used; the other is the singleton implementation. For exampleVariablePut it in AppDelegate as a globalVariableAppDelegate is a singleton class)

In Objective-C, how does one implement static member variables like in C ++?

What you need to do is in the implementation (. m or. mm. 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 scope of static variables is limited to a single file. The code can be as follows:

 
 
  1. //example.h     
  2. @interface Example : NSObject {    
  3.     
  4. }    
  5.     
  6. - (id)init;    
  7. +(int)instanceCount;    
  8.     
  9. @end    
  10.     
  11. //example.m     
  12. #import "example.h"     
  13.     
  14. static int count;    
  15.     
  16. @implementation Example    
  17. -(id)init{    
  18. self = [super init];    
  19. if(nil!=self){    
  20. count+=1;    
  21. }    
  22. return self;    
  23. }    
  24.     
  25. +(int)instanceCount{    
  26. return count;    
  27. }    
  28.     
  29. @end    
  30. //example.h  
  31. @interface Example : NSObject {  
  32.  
  33. }  
  34.  
  35. - (id)init;  
  36. +(int)instanceCount;  
  37.  
  38. @end  
  39.  
  40.  
  41. //example.m  
  42. #import "example.h"  
  43.  
  44. static int count;  
  45.  
  46. @implementation Example  
  47. -(id)init{  
  48. self = [super init];  
  49. if(nil!=self){  
  50. count+=1;  
  51. }  
  52. return self;  
  53. }  
  54.  
  55. +(int)instanceCount{  
  56. return count;  
  57. }  
  58. @end 

In the above Example, you can use [Example instanceCount]Static variablesYou do not need to create an instance.

Summary: DetailsObjective-CMediumStatic variablesI hope this article will help you with the introduction of the usage method.

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.