我相信大家从OC转到Swift的时候总会有这个困惑“以前在OC这样用,Swift该怎么写?”
多的不说了往下看。
1.懒加载
OC:
-(UITableView *)tableView{ if (_tableView == nil) { UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView = tableView;
_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.backgroundColor = [UIColor grayColor];
} return _tableView;
}
Swift
在定义时 增加
lazy
关键字lazy var tableView = UITableView()
也可以这样
lazy var tableView: UITableView = { [weak self] in
let tableView = UITableView()
tableView.frame = (self?.view.frame)!
tableView.backgroundColor = UIColor.black
return tableView
}()
但是不建议使用第二种方法,因为第二种方法是一个闭包,很容易循环引用,使用
[weak self]
又得考虑解包的问题,推荐第一种写法。
2.重写属性setter方法
OC
在用OC开发的时候经常会重写某个属性的setter方法来给子控件赋值
-(void)setMeMenu:(MeMenu *)meMenu{
_meMenu = meMenu; self.nameLabel.text = meMenu.name; self.iconView.image = [UIImage imageNamed:meMenu.iconName];
}
Swift
在定义属性时增加
didSet
PS:没有智能提示
var meMenu: meMenu?{
didSet{
nameLabel.text = meMenu!.name;
}
}
3.重写frame的setter方法
OC
在OC开发中大家都有过重写cell frame的需求
-(void)setFrame:(CGRect)frame
{
frame.origin.y += 10;
frame.size.height -= 10;
[super setFrame:frame];
}
Swift
override var frame:CGRect{
didSet {
var newFrame = frame
newFrame.origin.x += 10
newFrame.size.width -= 10 * 2
newFrame.origin.y += 10
newFrame.size.height -= 10 * 2
super.frame = newFrame
}
}
4.字典转模型
OC
@implementation MeMenu-(instancetype)initWithDic:(NSDictionary *)dic{ self = [super init]; if (self) { self.iconName = dic[@"icon"]; self.name = dic[@"name"];
} return self;
}
+(instancetype)initWithDic:(NSDictionary *)dic{ return [[self alloc]initWithDic:dic];
}
+(NSMutableArray *)meMenus{ NSString *path = [[NSBundle mainBundle] pathForResource:@"dataArr" ofType:@"plist"]; NSArray *arr = [NSArray arrayWithContentsOfFile:path]; NSMutableArray *arrs = [NSMutableArray array]; for (NSDictionary *dic in arr) {
[arrs addObject:[MeMenu initWithDic:dic]];
} return arrs;
}@end
Swift
class MeMenu: NSObject { var name: String? var type: String? var detail: String? var icon: String? init(dic: [String: String]) { super.init()
setValuesForKeys(dic)
} //外面调用这个类方法
class func meMenus() -> Array<Any>{ let arrDic = NSArray(contentsOfFile: Bundle.main.path(forResource: "DoctorList.plist", ofType: nil)!)! var arrayM = Array<MeMenu>() for dic in arrDic { let doctor = MeMenu(dic: dic as! [String : String])
arrayM.append(doctor)
} return arrayM
}
}
5.Swift的extension
和convenience
关键字
extension(扩展) 就是为一个已有的类、结构体、枚举类型或者协议类型添加新功能。这包括在没有权限获取原始源代码的情况下扩展类型的能力(即 逆向建模 )。扩展和 Objective-C 中的Category(分类)类似。(与 Objective-C 不同的是,Swift 的扩展没有名字。)
Swift 中的扩展可以:
添加计算型属性和计算型类型属性
定义实例方法和类型方法
提供新的构造器
定义下标
定义和使用新的嵌套类型
使一个已有类型符合某个协议
在 Swift 中,你甚至可以对协议进行扩展,提供协议要求的实现,或者添加额外的功能,从而可以让符合协议的类型拥有这些功能。你可以从协议扩展获取更多的细节。
注意
扩展可以为一个类型添加新的功能,但是不能重写已有的功能。
使用extension分割代码
可以让代码更易阅读与修改
class DempViewController: UIViewController {
lazy var tableView = UITableView() override func viewDidLoad() { super.viewDidLoad()
setupUI()
}
}// MARK: 设置界面extension DempViewController{
fileprivate func setupUI(){
tableView.dataSource = self
tableView.delegate = self
tableView.frame = view.bounds
}
}// MARK: 表格的数据源方法extension DempViewController: UITableViewDataSource{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 2
} func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "id", for: indexPath) return cell
}
}// MARK: 表格的代理方法extension DempViewController: UITableViewDelegate{
}
使用convenience便利构造器快速创建控件
新建 UIButton+Extension.swift 文件
建立 UIButton 的便利构造函数
extension UIButton {/// 快速创建按钮/// - parameter title: title/// - parameter imageName: imageName/// - parameter backImageName: backImageName////// - returns: UIButtonconvenience init(title: String, imageName: String, backImageName: String) { self.init()
setTitle(title, for: .normal)
setImage(UIImage(named: imageName), forState: .Normal)
setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Highlighted)
}
}
注意:便利构造器必须先调用self.init()而且没有智能提示~
开发中多利用extension
分割代码、抽取常用代码
OC中的Category
文/SoolyChristina(简书作者)
简书文章作者不定期的更新较有质量的干货,大家可以关注下
原文链接:http://www.jianshu.com/p/08fb33a346c6