• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Swift编码总结4 - 玉思盈蝶

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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!
    }
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift编码总结9 - 玉思盈蝶发布时间:2022-07-13
下一篇:
使用Swift代码演示Cocoa框架发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap