美文网首页selector
iOS中常见的关键字static 和class

iOS中常见的关键字static 和class

作者: 般若_781a | 来源:发表于2020-03-08 13:09 被阅读0次

static在iOS中的作用包括OC和Swift,并讲解常用的情况.代码主要以OC为主,Swift中主要描述和另外一个关键字class的异同

当static修饰局部变量时

变量在内存中的位置: 当用static修饰局部变量时,局部变量的内存地址会从栈变为全局区(静态区)

作用域:只在函数内部可见,只初始化一次,所以也只有一个内存地址

生命周期程序结束时才释放

- (void)viewDidLoad {

    [super viewDidLoad];

    [self howOldareyou];

    [self howOldareyou];

    [self howOldareyou];

    static int age = 10;//使用static修饰的局部变量,在其他的函数中声明相同变量名的变量时,是一个全新的变量

NSLog(@"age===%d",age);

    // Do any additional setup after loading the view, typically from a nib.

}

- (void)howOldareyou {

    static int age = 30;

    age--;

NSLog(@"age===%d",age);

}

// 控制台的输出结果

2018-11-29 22:46:31.602384+0800 static[1260:222387] age===29

2018-11-29 22:46:31.602557+0800 static[1260:222387] age===28

2018-11-29 22:46:31.602672+0800 static[1260:222387] age===27

2018-11-29 22:46:31.602758+0800 static[1260:222387] age===10

//每次的输出结果都不一样,被static修饰的局部变量内存地址只有一份,只被初始化一次,所有第二次调用howOldareyou方法时age没有被重新初始化,所以是29,29--之后就是输出28了

举例:常用情况讲解:我们经常在tableViewcell复用的时候使用到static,为什么需要在局部变量identifier之前加上static.我们先看看常用的写法

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {

static NSString * identifier = @"cellIdentifier";

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if(!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

    }

returncell;

}

tableview的这个代理方法是一个会被反复调用的方法,当identifier的前面不加static修饰时,identifier这个临时变量是储存在栈中的,指向常量区中的@"cellIdentifier",一旦这个代理方法执行完毕,identifier这个局部变量就会被回收.当再次调用的时候又会重新生成一个局部变量重新指向常量区的@"cellIdentifier".,消耗内存.

当使用static修饰identifier时,identifier的内存就会被分配在全局区(静态区),生命周期会变成这个程序的运行时间,不会随着代理方法调用完毕而销毁,一直指向常量区的@"cellIdentifier".当再次调用时不需要重新生成.节省内存

当static修饰全局变量时

内存中的位置 :仍然是在静态储存区没变,生命周期为整个程序运行期间.

作用域 :在整个声明它的文件中可用,在声明他之外的文件之外不可见

常用情况:iOS中的单例中的全局变量会用static修饰

@implementation Singleton

static Singleton * instance = nil;

+ (Singleton *)getInstance{

if(instance == nil) {

        instance = [[Singleton alloc] init];

    }

returninstance;

}

+ (id) allocWithZone:(struct _NSZone *)zone{

if(instance == nil) {

        instance = [super allocWithZone:zone];

    }

returninstance;

}

- (id) copyWithZone :(struct _NSZone*)zone{

returninstance;

}

为什么一定要用static 来修饰变为一个静态变量,而不是写成一个实例变量.因为单例是程序生命周期中唯一的实例,为确保实例化的唯一.而利用类的类方法来生成,而在类方法中不可以使用实例对象的变量,只能使用属于类的(static)类变量(一般在OC中没有特意区分static变量和类变量的区别).而且在声明它之外的文件不可见.

    /**

    在其他的类中实用extern来访问

    */

    extern Singleton * instance ;

NSLog(@"instance====%@",instance);

    直接无法编译过会直接报错

Undefined symbolsforarchitecture x86_64:

"_instance", referenced from:

-[ViewController viewDidLoad]inViewController.o

ld: symbol(s) not foundforarchitecture x86_64

clang: error: linkercommandfailed withexitcode 1 (use -v to see invocation)

复制代码

在Swift中static的使用(包含与关键字class的异同)

说道Swift中的static那就不得不说到另一个关键字class,在Swift中static和class都是用来描述类型作用域这一概念.两者都可以用来修饰计算类型.都可以用来修饰方法,static修饰的方法叫做静态方法,class修饰的是类方法.(在OC中我们不会特别的区分类方法,类变量,静态方法,静态变量),但是在Swift中class和static,无论是能够修饰的范围还是修饰后产生的效果区别都很大,不能混为一谈.

class 和 static相同点

都可以在class中修饰方法,static修饰的叫做静态方法,class修饰的叫做类方法.

都可以修饰class中的计算属性

class Programmer :NSObject {

    static var name : String{

return"老马"

    }

    class var nickname :String {

return"iOS"

    }

    class func writeTheCode() -> Void {

print("写代码")

    }

    static func fixTheBug() -> Void {

print("修复bug")

    }

}

复制代码

class 和static中的不同点

首先class修饰的属性和方法可以被子类重写,但是static不能被重写 

 class修饰的类方法被重写时,可以使用static修饰,让方法变成静态方法,但它的子类就不能重写了 class修饰的计算属性被重写时,可以使用static修饰,让其变成静态变量,但它的子类就不能重写了

class iOSProgrammer :Programmer{

    // 重写计算属性 可以使用static 来重写,但是static 重写后,就不能被它的子类再次重写了

    static override var nickname: String{

return"iOS"

    }

    // 重写类方法时可以使用static 让其变成静态方法

    static override func writeTheCode() -> Void {

print("写代码")

    }

}

其次class 和static能够修饰的范围不一样,class只能在class中修饰,而static可以不仅可以作用于class中,也可以在enum,和struct中使用.

相关文章

网友评论

    本文标题:iOS中常见的关键字static 和class

    本文链接:https://www.haomeiwen.com/subject/pmeddhtx.html