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
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, 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.
Help
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 with 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.
Help
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