1:枚举
枚举值有整型(Integer)、浮点数(Float Point)、字符串(String)、布尔类型(Boolean)

enum WeekDays: Int {
    case Sunday = 0
    case Monday = 1
    case Saturday = 2
}
let sunday = WeekDays.Sunday

enum MessageType: String {
    case TEXT = "text"
    case FAQ = "faq"
    case AUDIO = "audio"
}
let textStr = MessageType.TEXT.rawValue

2:结构体

struct serverDefine {
    static let NetWorkUrl = "http://www.baidu.com"
    static let IPHONE_WIDTH: CGFloat = UIScreen.main.bounds.size.width
    static func getUrl(api: String) -> String {
        return serverDefine.NetWorkUrl
    }
    static var StatusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.height
    static var NavigationBarHeight: CGFloat {
        get{
            return StatusBarHeight + 44.0
        }
    }
    static var TabBarHeight: CGFloat {
        get{
            var bottomInset: CGFloat = 0
            if #available(iOS 11, *){
                if let window = UIApplication.shared.keyWindow {
                    bottomInset = window.safeAreaInsets.bottom
                }
            }
            return bottomInset + 49.0
        }
    }
}

3:泛型
3.1:泛型函数:

private func printValue<T>(_ value: T) {
    switch value {
    case is String:
        print("我是String 类型")
        break
    case is NSNumber:
        print("我是NSNumbr类型")
        break
    default:
        break
    }
}

3.2:泛型结构体

struct stack<Element> {
    var items = [Element]()
    mutating func push(_ item: Element) {
        items.append(item)
    }
    mutating func pop() -> Element {
        return items.removeLast()
    }
}

4:代理模式

protocol TestVCDelegate : NSObjectProtocol {
    func saveName(_ nameStr : String)
}

class TestVC: UIViewController {
    weak var delegate : TestVCDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    func btnSureAction(_ sender: Any) {
        if self.delegate != nil && (self.delegate?.responds(to: Selector.init(("saveName:"))))!{
            self.delegate?.saveName("123456")
        }
    }
}

5:单利 (注意:一定要使init方法私有化)

private override init() {}
static let testDefault = TestNSObject()
public class var sharedInstance: TestNSObject {
    return testDefault
}

6:set get willSet didSet 方法

static var TabBarHeight: CGFloat {
    get {
        var bottomInset: CGFloat = 0
        if #available(iOS 11, *){
            if let window = UIApplication.shared.keyWindow {
                bottomInset = window.safeAreaInsets.bottom
            }
        }
        return bottomInset + 49.0
    }
}
var homeData = [String: Any]() {
    didSet {
        if let homeDataInfo = homeDataInfo {
            homeDataInfo(homeData)
        }
    }
}

7:deinit方法

和OC中的dealloc 一样的,通常在deinit和dealloc中需要执行的操作有:
对象销毁
KVO移除
移除通知
NSTimer销毁

8:extension扩展

public extension UILabel {
    
    class func commonLabelWithFont(_ font: CGFloat, text: String?, color: UIColor, textAlignment: NSTextAlignment) -> UILabel {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: font)
        label.text = text
        label.textColor = color
        label.textAlignment = textAlignment
        return label
    }
}

9:懒加载

private lazy var titleLabel: UILabel = {
    let label = UILabel()
    label.font = UIFont.systemFont(ofSize: 17*UIConstants.NewScale_iPhone6)
    return label
}()