代理协议 protocol

public protocol DelegateViewDelegate : NSObjectProtocol{

    func click(index:Int) -> Void
}

class DelegateView: UIView{

    weak var mydelegate : DelegateViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)

        let view1 = UIView(frame: CGRect(x: 0, y: 0, 
            width: frame.size.width/2, height: frame.size.height))
        view1.backgroundColor = UIColor.blue
        view1.isUserInteractionEnabled = true
        view1.tag = 1000
        let tap1=UITapGestureRecognizer(target: self, action: #selector(view1Click))
        view1.addGestureRecognizer(tap1)
        self.addSubview(view1)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc func view1Click() -> Void {
        mydelegate?.click(index: 1)
    }

}

block 闭包


//可以定义block(闭包)类型
typealias myBlock = (Int) -> (Int)
//或者直接定义
var bibao:((Int) -> ())?

//定义页面调用
bibao!(1)


//使用页面回调        
blockView?.bibao = {
    (a) in

    print(a)
    
    //如果有返回值的话
    //return a+1
}

单利


//创建单利属性
static let danli = DanLi()
private override init(){}

//类方法返回单利
class func shared() -> DanLi {
    return danli
}

//官方推荐写法
static let sharedInstence:DanLi = {
    let danli = DanLi()

    //danli.xxxxxx=xxxx
    //danli.function()

    return danli
}()

//属性
var name:String?

//方法
public func doSomthing() -> Void {
    print("------单利方法-----")
}


let danli = DanLi.danli
let danli02 = DanLi.shared()


danli.name = "danli-1"
print(danli02.name!)

danli02.name = "danli-2"
print(danli.name!)


danli.doSomthing()
danli02.doSomthing()


KVO


//定义

class MyKVO: NSObject {

    @objc dynamic var name: String

    init(name: String) {
        self.name = name
    }
}

class MyKVO1: NSObject {

    @objc dynamic var name: String

    override init() {
        self.name = ""
    }
}

//MARK: - 第一种KVO block回调。不用手动删除监听者
kvoClass = MyKVO(name: "kvo")
ob = kvoClass.observe(\.name) { (ob, changed) in
    let new = ob.name
    print("name changed: \(new)")
}
kvoClass.name = "swift4"


//MARK: - 第二种KVO 调用方法
kvoClass1 = MyKVO1()
kvoClass1.addObserver(self, forKeyPath: "name", options: .new, context: &myContext)
kvoClass1.name = "swift4"

override func observeValue(forKeyPath keyPath: String?, of object: Any?, 
    change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

    if context == &myContext {
        if let newValue = change?[NSKeyValueChangeKey.newKey] {
            print("name changed: \(newValue)")
        }
    } else {
        super.observeValue(forKeyPath: keyPath, of: object, change: change,
            context: context)
    }
}

通知



//发送通知
let notificationName =  NSNotification.Name(rawValue: "MyNotification")
NotificationCenter.default.post(name: notificationName, object: self, 
    userInfo: ["value1":"11","value2":22])


//接收通知
let notificationName = Notification.Name(rawValue: "MyNotification")
NotificationCenter.default.addObserver(self, selector: #selector(
    receiveNotification(notification:)), name: notificationName, object: nil)

@objc func receiveNotification(notification: Notification) -> Void {

    let userInfo = notification.userInfo as! [String: AnyObject]
    
    print(userInfo)
    
    let value1 = userInfo["value1"] as! String
    let value2 = userInfo["value2"] as! Int
    print(value1)
    print(value2)
    
    sleep(3)
    
    print("执行完毕")
    
}
    
deinit {
    //记得移除通知监听
    NotificationCenter.default.removeObserver(self)
}

userDefaults


let userDefaults = UserDefaults.standard

//存
userDefaults.set("一条数据", forKey: "key")
//取
let value = userDefaults.value(forKey: "key") as? String
//删
userDefaults.removeObject(forKey: "key")


常用的几种传值方式