Swift编码总结4

Posted pengsi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift编码总结4相关的知识,希望对你有一定的参考价值。

1.swift @discardableResult 声明:

swift正常的方法如果有返回值的话,调用的时候必须有一个接收方,否则的话编译器会报一个警告,
如果在方法前加上 @discardableResult 不处理的时候就不会有警告了。也可以用一个通配符接收方法返回值,可以达到同样的目的。

2.控制器初始化:

init(urlStr: String?) {
        super.init(nibName: nil, bundle: nil)
        if let urlStr = urlStr {
            self.urlStr = urlStr
        }
    }
UIView初始化:调用父类initWithFrame

3.更新View的约束:

/// 更新二级菜单约束
    private func updateSecondCategoryLayout(){
        if let tempHeight = height {
            secondCategoryView.needsUpdateConstraints()
            tempHeight.update(offset: 42)
            UIView.animate(withDuration: 0.25) {
                self.secondCategoryView.layoutIfNeeded()
            }
        }
    }

4.上拉加载更多:

printTable.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: { [weak self] in
            guard let `self` = self else {return}
            if self.pageNum <= self.totalPage {
                self.loadData()
            } else {
                self.printTable.mj_footer.endRefreshing()
                self.printTable.mj_footer.endRefreshingWithNoMoreData()
            }
        })

5.UIView.performWithoutAnimation:

UIView.performWithoutAnimation {
                    planDiscountCollectionView.reloadSections(IndexSet(integer: IndexSet.Element(0)))
                }

//视图转换时不执行动画,参数为block对象,来执行你要执行的代码
+ (void)performWithoutAnimation:(void (^)(void))actionsWithoutAnimation

6.十进制转换:

if tempTextCount.decimalValue > NSDecimalNumber(string: WaiterManager.shared.waiterPower(PowerCode.ignore)?.powerValue).decimalValue {
}

7.限制textField只能输入3个字符:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        let currentCharacterCount = textField.text?.count ?? 0
        let newLength = currentCharacterCount + string.count - range.length
        return newLength <= 3
    }

8.变量安全使用使用set方法private,可以调方法改变值:

class HHTComboGlobalData: NSObject {
    static let shared = HHTComboGlobalData()

    private(set) var comboModes:[ComboModel] = []
    
    func setComboModel(with dicts:[JSONDictionary]) {
        comboModes.removeAll()
        comboModes = dicts.flatMap { (dict) -> ComboModel? in
            return ComboModel(with: dict)
        }
    }
}

9.for循环创建九宫格:

private func setupUI() {
        let count: Int = 5
        let width: CGFloat = 100
        let height: CGFloat = 50
        let bgViewWidth: CGFloat = 844
        let col: Int = 3
        // 间距
        let hMargin = (bgViewWidth - (CGFloat(col) * width)) / CGFloat((col+1))
        let vMargin:CGFloat = 20
        
        var row:Int = 0
        for i in 0..<count {
            let button: UIButton = UIButton()
            button.setTitle("打印一", for: .normal)
            button.setTitleColor(UIColor.white, for: .normal)
            button.backgroundColor = UIColor.red
            if i%col == 0 {
                row += 1
            }
            var y: CGFloat = 0
            if row == 1 {
                y = 50
            } else {
                y = vMargin + (height + vMargin) * CGFloat(row)
            }
            let x:CGFloat = hMargin + (width + hMargin) * CGFloat(i%col)
            button.frame = CGRect(x: x, y: y, width: width, height: height)
            bgView.addSubview(button)
            bottomMargin = button.frame.maxY + 50
        }
        bgHeightConstraint.constant = bottomMargin!
    }
}

以上是关于Swift编码总结4的主要内容,如果未能解决你的问题,请参考以下文章

Swift编码总结9

Swift编码总结1

Swift编码总结5

Swift编码总结7

Swift编码总结8

swift常用代码片段