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.