zoukankan      html  css  js  c++  java
  • extension的作用

    一、在类中写

    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
        }
    }
  • 相关阅读:
    powerdesigner添加mysql的字符集ENGINE和DEFAULT CHARACTER SET
    powerdesigner怎么设置同时显示name和code
    更改gradle的java的class文件输出目录的结构
    使用TortoiseGit时如何实现SSH免密码登录
    TortoiseGit之配置密钥
    Mock InjectMocks ( @Mock 和 @InjectMocks )区别
    Centos tomcat jmx 远程连接
    【C++】常见易犯错误之数值类型取值溢出与截断(3)
    【C++】常见易犯错误之数值类型取值溢出与截断(2)
    【C++】常见易犯错误之数值类型取值溢出与截断(1)
  • 原文地址:https://www.cnblogs.com/iOS363536404/p/5984103.html
Copyright © 2011-2022 走看看