码迷,mamicode.com
首页 > 编程语言 > 详细

Swift编码总结4

时间:2018-04-28 14:02:10      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:oat   range   dex   tle   fun   限制   打印   二级菜单   必须   

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

标签:oat   range   dex   tle   fun   限制   打印   二级菜单   必须   

原文地址:https://www.cnblogs.com/pengsi/p/8966611.html

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