学习ios时候经常遇到有static修饰的变量和没有static的变量,在iOS中主要用到它们之间的区别是:如 static NSString *identify = @"identify";与NSString *identify = @"identify";1、作用范围不一样,有static修饰的identity的内容表明是全局变量,储存在静态数据区;2、最重要的区别在于:下面代码在初始化tableView的cell的时候
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
//NSString *identify = @"identify";
//static NSString *identify = @"identify";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
if (nil == cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indentfy];
}
带有static修饰的标识符,在cell重用的时候不会重复声明,只是声明一次;而没有static修饰的标识符,在cell重用的时候每重用一次都要重新声明一下标识符,假如有几十万个cell,如果声明标识符没有static修饰,可能发生因为大量内存被占用而crash掉。