美文网首页
2016.11.16

2016.11.16

作者: dslCoding | 来源:发表于2016-11-16 17:26 被阅读7次

NSString* urlStr = [strfstringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSetcharacterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];

在程序设计中,我们经常会使用懒加载,顾名思义,就是用到的时候再开辟空间,比如iOS开发中的最常用控件UITableView,实现数据源方法的时候,通常我们都会这样写

Objective-C

- (NSInteger)tableView:(UITableView *)tableView  numberOfRowsInSection:(NSInteger)section

{

return self.dataArray.count;

}

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

//1.得到cell

XWShopCell *cell = [XWShopCell cellWithTableView:tableView];

//2.传递模型

cell.wine = self.dataArray[indexPath.row];

//3.回传cell

return cell;

}

上面的的代码中

return self.dataArray.count;

其实就是利用

@property (nonatomic, strong) NSArray *dataArray;

@property 的特性,为属性生成了get和set方法,而这里是调用的get方法,但是上述代码中return self.dataArray.count 会调用

- (NSArray *)dataArray{ return _dataArray}

这样调用,如果成员属性dataArray 开始没有赋值的,那么在使用的时候,调用get方法,不重写的话,会报错,空指针,所以一般我们会重写get方法

//重写get方法

- (NSArray *)dataArray

{

if (nil == _dataArray){

_dataArray = [NSArray array];

}

return _dataArray

}

这样写,就防止了成员属性为没有赋值的情况

综上所述,Objective-C的懒加载,其实就是调用成员属性的get方法,初始化值,而Swift的懒加载,是和Objective-C不同的

Swift

//MARK tablview的 dataSource 代理方法

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

return self.dataArray.count

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

//1.得到cell

let cell = XWShopCell.cellWithTableView(tableView)

//2.传递模型

cell.wine = self.dataArray[indexPath.row]

//3.回传cell

return cell

}

而这句

return self.dataArray.count

在Swift 存储属性必须初始化,确认类型,或者用可选类型,总之要确认类型,毕竟Swfit是类型安全语言,所以Swift提出了lazy属性,用法

//1.分析 NSArray 是一个闭包的返回值,而这是一个没有参数的闭包

lazy var dataArray:NSArray = { [] }()

//2.也可以写成这样 lazy var dataArray:NSArray = { return NSArray() }()

//3.从plist文件加载

lazy var dataArray:Array = {

let winePath = NSBundle.mainBundle().pathForResource("wine.plist", ofType: nil)!

let winesM = NSMutableArray(contentsOfFile: winePath);

var tmpArray:Array! = []

for tmpWineDict in winesM! {

var wine:XWWine = XWWine.wineWithDict(tmpWineDict as! NSDictionary)

tmpArray.append(wine)

}

print("我就运行一次")

return tmpArray }()

上述的代码,有点难理解,如果之前会Objective-C的block 或者对C语言的函数指针理解透彻的,可以看成是一个代码块,然后self.dataArray的时候,就执行了代码块,但是重复调用,Lazy 属性的代码块只会调用一次,lazy修饰的是一个存储属性,而存放的是闭包,我想内部,应该进行了优化。

#import"ViewController.h"#import"AFNetworkReachabilityManager.h"@interfaceViewController ()@property(nonatomic, strong) NSURLSessionDownloadTask *downloadTask;// 下载任务@property(nonatomic, strong) NSURLSession *session;// 会话@property(nonatomic, strong) NSData *resumData;// 续传数据@property(nonatomic, strong) NSDate *lastDate;// 上次获取到数据的时间./** 两个label都是用Storybord创建的. */@property(weak, nonatomic) IBOutlet UILabel *progressLabel;@property(weak, nonatomic) IBOutlet UILabel *speedLabel;@end@implementationViewController

@end

相关文章

  • 我的日记04

    2016.11.16 星期三 ...

  • 2016.11.16

    1 你有时很重 可轻轻一跳 就坐上树枝 你有时很轻 可刚要起飞 就坠楼身亡 2 那个嚼过石头的人 在啃一块面包时 ...

  • 2016.11.16

    Another busy day Today is the third all day in hospital.B...

  • 2016.11.16

    别人稍一注意你,你就敞开心扉,你以为这是坦率,其实这是孤独。 如果一个人从小就特别乖,那她一定没什么人爱。

  • 2016.11.16

    这几天一直想着要不要试着记录一下一些琐事。刚刚立刻百度了一下,选择了这里。希望我可以用文字记下我想写下的故事。 早...

  • 2016.11.16

    王晓梅,为了你的目标去想任何能够达到的办法,10个计划我一定可以做到的,2016年11月份,从今天开始。

  • 2016.11.16

    昨天晚上看了一篇关于培养出一名优秀女儿的文章,当然每个人对优秀的定义都不一样。不过这篇文章我很赞同里面的好些方法。...

  • 2016.11.16

    昨天下午逛了一下午街...感觉逛完了所有我心心念念的地方 随着淘宝双十一购买的东西逐渐收到 心也开始慢慢放下来 开...

  • 2016.11.16

    NSString* urlStr = [strfstringByAddingPercentEncodingWith...

  • 2016.11.16

网友评论

      本文标题:2016.11.16

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