• 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
        }
    }
  • 相关阅读:
    JVM字节码-字节码进阶
    JVM字节码-Class文件结构
    CT03 Contest#10 equation 大力打表+讨论
    2021CT03 Contest#9 降智场
    妙妙题 noi.ac 2323 Connecting
    洛谷 P4774 [NOI2018] 屠龙勇士
    [模板] 扩展中国剩余定理
    洛谷 P1082 [NOIP2012 提高组] 同余方程
    洛谷 P1516 青蛙的约会
    2021牛客暑期多校训练营3 Kuriyama Mirai and Exclusive Or
  • 原文地址:https://www.cnblogs.com/iOS363536404/p/5984103.html
Copyright © 2020-2023  润新知