码迷,mamicode.com
首页 > 其他好文 > 详细

extension的作用

时间:2016-10-21 13:40:38      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:color   gpo   需要   构造   class   code   log   elf   print   

一、在类中写

1. 比如在MainViewController类中,写一个extension 方法,这个方法里面尽量不要写成如下格式,因为会报错,找不到在clas

中创建的private lazy var 对象

// 相当于分类
extension MainTabBarController{
    private func setAddButton(){
        
        tabBar.addSubview(addBtn)
        
        // 3.设置位置
        addBtn.center =  CGPoint(x: tabBar.center.x, y: tabBar.bounds.size.height * 0.5)
    }

}

报错信息是:

Use of unresolved identifier ‘addBtn‘,

使用extension 的目的就是解耦合,但是当程序员这样写的时候,就会增加耦合,所以不建议这样写。

 

二、在类中可以使用extension来进行数据源的扩展

比如tableview,就可以使用这个进行扩展。代码如下:

// MARK:- tableView的数据源和代理方法
// extension类似OC的category,也是只能扩充方法,不能扩充属性
extension ViewController : UITableViewDataSource, UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // 1.创建cell
        let CellID = "CellID"
        var cell = tableView.dequeueReusableCell(withIdentifier: CellID)
        
        if cell == nil {
            // 在swift中使用枚举: 1> 枚举类型.具体的类型 2> .具体的类型
            cell = UITableViewCell(style: .default, reuseIdentifier: CellID)
        }
        
        // 2.给cell设置数据
        cell?.textLabel?.text = "测试数据:\((indexPath as NSIndexPath).row)"
        
        // 3.返回cell
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("点击了:\((indexPath as NSIndexPath).row)")
    }
}

 

三、 遍历构造函数

比如:UIButton.

可以创建一个类,把这个类的导入改成 : import UIKit

代码如下:

import UIKit

extension UIButton {
   
    // convenience : 便利,使用convenience修饰的构造函数叫做便利构造函数
    // 遍历构造函数通常用在对系统的类进行构造函数的扩充时使用
    /* 
     遍历构造函数的特点
        1.遍历构造函数通常都是写在extension里面
        2.遍历构造函数init前面需要加载convenience
        3.在遍历构造函数中需要明确的调用self.init()
     */
    convenience init (imageName : String, bgImageName : String) {
        self.init()
        
        setImage(UIImage(named: imageName), forState: .Normal)
        setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
        setBackgroundImage(UIImage(named: bgImageName), forState: .Normal)
        setBackgroundImage(UIImage(named: bgImageName + "_highlighted"), forState: .Highlighted)
        sizeToFit()
    }
}

 

四、其他:

import UIKit

extension UIButton {
    // swift中类方法是以class开头的方法.类似于OC中+开头的方法
    
    class func createButton(imageName : String, bgImageName : String) -> UIButton {
        // 1.创建btn
        let btn = UIButton()
        
        // 2.设置btn的属性
        btn.setImage(UIImage(named: imageName), forState: .Normal)
        btn.setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
        btn.setBackgroundImage(UIImage(named: bgImageName), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: bgImageName + "_highlighted"), forState: .Highlighted)
        btn.sizeToFit()
        
        return btn
    }
}

 

extension的作用

标签:color   gpo   需要   构造   class   code   log   elf   print   

原文地址:http://www.cnblogs.com/iOS363536404/p/5984103.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!