swift里类方法和构造方法的使用来减少代码冗余提高开发效率
Posted lizx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift里类方法和构造方法的使用来减少代码冗余提高开发效率相关的知识,希望对你有一定的参考价值。
对于日常中重复性的可以抽取的实现尽量抽取 比如实现导航栏右侧
UIBarButtonItem的添加 如果使用
private func setupNavigateionBar(){
let btn = UIButton()
btn.setImage(UIImage(named:"icon_shouye_off"), for: .normal)
btn.sizeToFit()
navigationItem.leftBarButtonItem = UIBarButtonItem(customView:btn)
//右侧导航栏设置多个功能item
let firstbtn = UIButton()
firstbtn.setImage(UIImage(named:"icon_shouye_off"), for: .normal)
firstbtn.setImage(UIImage(named:"icon_shouye_on"), for: .highlighted)
//使用sizetofit会根据图片的大小动态适应改变item大小 要想自定义btn大小需使用自定义的size改变相邻按钮的间隔
let size = CGSize(width: 40, height: 40)
firstbtn.frame = CGRect(origin: CGPoint.zero, size: size)
let firstRightItem = UIBarButtonItem(customView:firstbtn)
let secondbtn = UIButton()
secondbtn.setImage(UIImage(named:"icon_shouye_off"), for: .normal)
secondbtn.setImage(UIImage(named:"icon_shouye_on"), for: .highlighted)
secondbtn.sizeToFit()
let secondRightItem = UIBarButtonItem(customView:secondbtn)
let thirdbtn = UIButton()
thirdbtn.setImage(UIImage(named:"icon_shouye_off"), for: .normal)
thirdbtn.setImage(UIImage(named:"icon_shouye_on"), for: .highlighted)
thirdbtn.sizeToFit()
let thirdbtnRightItem = UIBarButtonItem(customView:thirdbtn)
navigationItem.rightBarButtonItems = [firstRightItem, secondRightItem, thirdbtnRightItem]
}
实现添加三个右侧的导航栏Item 代码冗余比较严重就可以分别通过实现类方法和构造方法两种方式来实现抽取。
一、使用类方法
1.新建一个UIBarButtonItem-extension.swift文件
import UIKit
extension UIBarButtonItem{
//扩展一个类方法
class func createItem(imageName:String, highImageName : String,size : CGSize) ->UIBarButtonItem{
let btn = UIButton()
btn.setImage(UIImage(named: imageName), for: .normal)
btn.setImage(UIImage(named: highImageName), for: .highlighted)
btn.frame = CGRect(origin:CGPoint.zero, size:size)
return UIBarButtonItem(customView:btn)
}
}
上面的冗余方法即可优化为:
let firstRightItem = UIBarButtonItem.createItem(imageName: "icon_shouye_off", highImageName: "icon_shouye_on", size: size)//UIBarButtonItem(customView:firstbtn)
二、使用构造函数方法
//使用构造函数的方式实现上面的功能
//便利构造函数 1>必须以convenience开头 2>在构造函数中必须明确调用一个设计的构造函数,并且是self来调用的(self)3.构造函数不需要返回值
convenience init(imageName : String, highImageName: String, size : CGSize) {
let btn = UIButton()
btn.setImage(UIImage(named: imageName), for: .normal)
btn.setImage(UIImage(named: highImageName), for: .highlighted)
btn.frame = CGRect(origin:CGPoint.zero, size:size)
self.init(customView: btn)
}
上面的冗余方法即可优化为: let secondRightItem = UIBarButtonItem(imageName : "icon_shouye_off", highImageName: "icon_shouye_on", size : size)///UIBarButtonItem(customView:secondbtn)
继续优化给构造函数参数添加默认参数及逻辑判断
convenience init(imageName : String, highImageName: String = "", size : CGSize = CGSize.zero) {
//1.创建button
let btn = UIButton()
//2.设置button图片
btn.setImage(UIImage(named: imageName), for: .normal)
if highImageName != ""{
btn.setImage(UIImage(named: highImageName), for: .highlighted)
}
//3.设置button的尺寸
if size == CGSize.zero
{
btn.sizeToFit()
}else{
btn.frame = CGRect(origin:CGPoint.zero, size:size)
}
//4.创建UIbarbuttonItem
self.init(customView: btn)
}
左侧的按钮可以优化为
navigationItem.leftBarButtonItem = UIBarButtonItem(imageName:"icon_shouye_off")//UIBarButtonItem(customView:btn)
以上是关于swift里类方法和构造方法的使用来减少代码冗余提高开发效率的主要内容,如果未能解决你的问题,请参考以下文章