1、定义
什么是静态变量:从面向对象的角度触发,当需要一个数据对象为整类而非某个对象服务,同时又力求不破坏类的封装性;既要求此成员隐藏在类的内部,又要求对外不可见的时候,就可以使用static。
2、作用
(1)函数体内 static 变量的作用范围为该函数体,不同于 auto 变量,在编译期就会初始化,也就是说这个静态变量值要么为nil,要么在编译期就可以确定其值,生命周期和程序相同,其内存只被分配一次并存储到全局变量区,因此其值在下次调用时仍维持上次的值;
(2)在模块内的 static 全局变量可以被模块内所用函数访问,但不能被模块外其它函数访问;
(3)在模块内的 static 函数只可被这一模块内的其它函数调用,这个函数的使用范围被限制在声明它的模块内;
(4)在类中的 static 成员变量属于整个类所拥有,对类的所有对象只有一份拷贝;
(5)在类中的 static 成员函数属于整个类所拥有,这个函数不接收 this 指针,因而只能访问类的static 成员变量。
修饰局部变量:
1.延长局部变量的生命周期,程序结束才会销毁。
2.局部变量只会生成一份内存,只会初始化一次。
3.改变局部变量的作用域。
修饰全局变量
1.只能在本文件中访问,修改全局变量的作用域,生命周期不会改
2.避免重复定义全局变量
3、优点
·节省内存。静态变量只存储一处,但供所有对象使用。
·它的值是可以更新的。
·可提高时间效率。只要某个对象对静态变量更新一次,所有的对象都能访问更新后的值
4、注意
1)静态方法只能调用静态方法和静态变量
2)如果.m文件和方法体里面定义了同名的static
变量,那么方法体里面的实例变量和全局的static变量不会冲突,在方法体内部访问的static变量和全局的static变量是不同的。
测试代码:
//
// AppDelegate.m
// Static_Demo
//
// Created by XDS on 16/6/24.
// Copyright © 2016年 xds. All
rights reserved.
//
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
static NSString *staticStr = @"test";
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
static NSString *staticStr
= @"test2";
NSLog(@"the staticStr is
%@ -- %lu", staticStr, (unsigned long)[staticStr hash]);
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the
application is about to move from active to inactive state. This
can occur for certain types of temporary interruptions (such as an
incoming phone call or SMS message) or when the user quits the
application and it begins the transition to the background
state.
// Use this method to
pause ongoing tasks, disable timers, and throttle down OpenGL ES
frame rates. Games should use this method to pause the game.
NSLog(@"the staticStr is
%@ -- %lu", staticStr, (unsigned long)[staticStr hash]);
}
- (void)applicationDidEnterBackground:(UIApplication
*)application {
// Use this method to
release shared resources, save user data, invalidate timers, and
store enough application state information to restore your
application to its current state in case it is terminated
later.
// If your application
supports background execution, this method is called instead of
applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication
*)application {
// Called as part of the
transition from the background to the inactive state; here you can
undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that
were paused (or not yet started) while the application was
inactive. If the application was previously in the background,
optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the
application is about to terminate. Save data if appropriate. See
also applicationDidEnterBackground:.
}
@end
运行后打印:
2016-06-24 16:40:27.865 Static_Demo[2926:2067070] the staticStr
is test2 -- 19618833822423