内容简介:本文说说static在iOS中的作用包括OC和Swift,并讲解常用的情况.代码主要以OC为主,Swift中主要描述和另外一个关键字class的异同tableview的这个代理方法是一个会被反复调用的方法,当identifier的前面不加static修饰时,identifier这个临时变量是储存在栈中的,指向常量区中的@"cellIdentifier",一旦这个代理方法执行完毕,identifier这个局部变量就会被回收.当再次调用的时候又会重新生成一个局部变量重新指向常量区的@"cellIdentifi
本文说说static在iOS中的作用包括OC和Swift,并讲解常用的情况.代码主要以OC为主,Swift中主要描述和另外一个关键字class的异同
当static修饰局部变量时
- 变量在内存中的位置: 当用static修饰局部变量时,局部变量的内存地址会从栈变为全局区(静态区)
- 作用域:只在函数内部可见,只初始化一次,所以也只有一个内存地址
- 生命周期程序结束时才释放
- (void)viewDidLoad {
[super viewDidLoad];
[self tobeYoung];
[self tobeYoung];
[self tobeYoung];
static int age = 20;//使用static修饰的局部变量,在其他的函数中声明相同变量名的变量时,是一个全新的变量
NSLog(@"age===%d",age);
// Do any additional setup after loading the view, typically from a nib.
}
- (void)tobeYoung {
static int age = 10;
age--;
NSLog(@"age===%d",age);
}
// 控制台的输出结果
2018-11-29 22:46:31.602384+0800 static[1260:222387] age===9
2018-11-29 22:46:31.602557+0800 static[1260:222387] age===8
2018-11-29 22:46:31.602672+0800 static[1260:222387] age===7
2018-11-29 22:46:31.602758+0800 static[1260:222387] age===20
//每次的输出结果都不一样,被static修饰的局部变量内存地址只有一份,只被初始化一次,所有第二次调用tobeYoung方法时age没有被重新初始化,所以是9,9--之后就是输出8了
复制代码
- 常用情况讲解:我们经常在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];
}
return cell;
}
复制代码
tableview的这个代理方法是一个会被反复调用的方法,当identifier的前面不加static修饰时,identifier这个临时变量是储存在栈中的,指向常量区中的@"cellIdentifier",一旦这个代理方法执行完毕,identifier这个局部变量就会被回收.当再次调用的时候又会重新生成一个局部变量重新指向常量区的@"cellIdentifier".,消耗内存.
当使用static修饰identifier时,identifier的内存就会被分配在全局区(静态区),生命周期会变成这个程序的运行时间,不会随着代理方法调用完毕而销毁,一直指向常量区的@"cellIdentifier".当再次调用时不需要重新生成.节省内存
当static修饰全局变量时
- 内存中的位置 :仍然是在静态储存区没变,生命周期为整个程序运行期间.
- 作用域 :在整个声明它的文件中可用,在声明他之外的文件之外不可见
- 常用情况:iOS中的单例中的全局变量会用static修饰
#import "Singleton.h"
@implementation Singleton
static Singleton * instance = nil;
+ (Singleton *)getInstance{
if (instance == nil) {
instance = [[Singleton alloc] init];
}
return instance;
}
+ (id) allocWithZone:(struct _NSZone *)zone{
if (instance == nil) {
instance = [super allocWithZone:zone];
}
return instance;
}
- (id) copyWithZone :(struct _NSZone*)zone{
return instance;
}
@end
复制代码
为什么一定要用static 来修饰变为一个静态变量,而不是写成一个实例变量.因为单例是程序生命周期中唯一的实例,为确保实例化的唯一.而利用类的类方法来生成,而在类方法中不可以使用实例对象的变量,只能使用属于类的(static)类变量(一般在OC中没有特意区分static变量和类变量的区别).而且在声明它之外的文件不可见.
/**
在其他的类中实用extern来访问
*/
extern Singleton * instance ;
NSLog(@"instance====%@",instance);
直接无法编译过会直接报错
Undefined symbols for architecture x86_64:
"_instance", referenced from:
-[ViewController viewDidLoad] in ViewController.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 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 "王重阳"
}
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中使用.
通篇有限今天暂时到此为此,如有错误,希望大家帮忙在评论中指出,欢迎大家来交流沟通.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Data Mining
Jiawei Han、Micheline Kamber、Jian Pei / Morgan Kaufmann / 2011-7-6 / USD 74.95
The increasing volume of data in modern business and science calls for more complex and sophisticated tools. Although advances in data mining technology have made extensive data collection much easier......一起来看看 《Data Mining》 这本书的介绍吧!