Swift细节记录<一>

1.全局变量记录:

import UIKit

class HHTSwitchGlobalData: NSObject {
    
    var isWaiterAutoPop: Bool = true 
    
    private(set) var isUseIntegral : Bool = true //是否使用积分抵现
   
    static let shared = HHTSwitchGlobalData()
    
    /// 更新全部开关数据的方法
    ///
    /// - Parameter allSwitchData: 全部开关数据
    func update(withSwitchDic dic: JSONDictionary) {
        
        isUseIntegral = Bool.boolValue(dic[ServerConfigKey.prepayUseScore.rawValue])
    }
    
}

2.元祖使用别名:

typealias OrderedDishModelTuple = (potViewModels: [DishViewModel], dishViewModels: [DishViewModel])

  typealias JSONDictionary = [String: Any]

public func fetchOrderedPotsAndDishes() -> OrderedDishModelTuple {
        let orderedPots = TableManager.shared.orderedDishViewModels.filter {  $0.dishModel.dishOrderType == .pot }
        let orderedDishes = TableManager.shared.orderedDishViewModels.filter { $0.dishModel.dishOrderType == .dish }
        return (orderedPots, orderedDishes)
    }

3.多使用Manager文件管理属性和方法:

多使用枚举,结构体。

Model可以使用结构体定义。

struct PowerModel {
    let powerID: String
    let powerName: String
    let powerCode: String
    let powerValue: String
    init(withDict dict: JSONDictionary) {
        powerID = String.stringValue(dict["powerId"])
        powerCode = String.stringValue(dict["powerCode"])
        powerName = String.stringValue(dict["powerName"])
        powerValue = String.stringValue(dict["powerValue"])
    }
}

4.公用的方法使用protocol:

import Foundation

protocol ModelProtocol: NSObjectProtocol {
    init(withDict dict: JSONDictionary)
}
class DishTip: NSObject, ModelProtocol{
    
    let tipID: String
    
    required init(withDict dict: JSONDictionary) {
        tipID = String.stringValue(dict["id"])

        super.init()
    }

5.多使用Extension实现公有,多次使用的方法;

6.

服务器IP使用NetConfig.swift文件单独实现,方便和服务器联调和调试;

接口使用一个枚举文件写在一起,方便查看和修改;

import Foundation
//MAKR: - 获取IP地址
class NetConfig {
    static var curServerIP: String{
        get{
          // return HHTServerConfigGlobalData.shared.bindIP

return ServerIP.localHost.getIP() } } static let timeInterval = 60 } enum ServerIP : String { case localHost = "127.0.0.1:9000" //本地 func getIP() -> String { return self.rawValue } }

7.控制器用Xib创建,代码改变控件值:

 @IBOutlet weak var sureBtn: UIButton! {
        didSet {
//            sureBtn.setTitle("确认发卡", for: .normal)
            sureBtn.setTitle("准备读卡,操作过程中请勿拿走卡片...", for: .disabled)
        }
    }

8.多使用private,在Extension要使用就filePrivate修饰;

   多使用??和?;

   多使用map,flatMap,filter

let amount = self.moneyTxt.text.flatMap{ Int($0) } ?? 0

9.可以在Model中修改值:

struct CardHistoryInfoModel {
    var cardNumber: String = ""
    var cardType: String = ""
    var cardTypeShowName: String {
        get {
            if cardType == "001" {
                return "储值卡"
            } else if cardType == "002" {
                return "赠卡"
            } else {
                return "未知类型"
            }
        }
    }
}

10.方法的跳转可以用Struct写在一起。

class NavigationBarView: UIView {
    
    struct Action {
        static let leftButtonClick = #selector(NavigationBarView.leftButtonClick)
        static let gotoPersonalCenter = #selector(NavigationBarView.gotoPersonalCenter)
    }
}

 

posted on 2018-03-27 21:01  玉思盈蝶  阅读(175)  评论(0编辑  收藏  举报

导航