iOS:OC开发中的写法与Swift中写法的对比

我相信大家从OC转到Swift的时候总会有这个困惑“以前在OC这样用,Swift该怎么写?
多的不说了往下看。

1.懒加载

  • Objective-C:
-(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 = { 
       let tableView = UITableView()
        tableView.frame = self.view.bounds
        tableView.backgroundColor = UIColor.black
        return tableView
    }()

但是不建议使用第二种方法,因为第二种方法本质是一个闭包,使用self.得考虑是否循环引用,使用[weak self]又得考虑解包的问题,而且闭包中智能提示不友好所以推荐第一种写法。

2.重写属性setter方法

  • Objective-C
    在用OC开发的时候经常会重写某个属性的setter方法来给子控件赋值
-(void)setMeMenu:(MeMenu *)meMenu{
    _meMenu = meMenu;
    self.nameLabel.text = meMenu.name;
    self.iconView.image = [UIImage imageNamed:meMenu.iconName];
}
  • Swift
    在定义属性时增加didSet意思是在属性set之后执行
    PS:没有智能提示
var meMenu: meMenu?{
        didSet{
            nameLabel.text = meMenu?.name;
        }
    }

3.重写frame的setter方法

  • Objective-C
    在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.字典转模型

  • Objective-C
@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() -> [Any] {
        let arrDic = NSArray(contentsOfFile: Bundle.main.path(forResource: "DoctorList.plist", ofType: nil)!)!
        var arrayM = [MeMenu]()
        for dic in arrDic {
            let doctor = MeMenu(dic: dic as! [String : String])
            arrayM.append(doctor)
        }
        return arrayM
    }
}

5.Swift的extensionconvenience关键字

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: UIButton
    convenience 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分割代码、抽取常用代码

  • Objective-C中的Category

《iOS:OC开发中的写法与Swift中写法的对比》 1.png

    原文作者:SoolyChristina
    原文地址: https://www.jianshu.com/p/08fb33a346c6
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞