1. 避免内存泄露
1)由于闭包引用了self而可能导致的内存泄露,必须如下这样写:
weak var weakSelf = self;
nearByVM = BMKNearByviewmodel(onResult: { (address) -> () in
weakSelf?.onGetAddress(address);
});
或者这样
nearByVM = BMKNearByviewmodel(onResult: {[weak self] (address) -> () in
self?.onGetAddress(address);
});
2)使用delegate代理循环引用可能导致的内存泄露,必须如下这样写:
weakvar delegate : BMKMapViewDelegate?
2. swift的枚举与oc的枚举比较
枚举值后加value
如
switch error.errorCode.value{case EMErrorServerNotReachable.value:}
3.It's the same as the Objective-C API,but uses Swift's Syntax.
NSNotificationCenter.defaultCenter().addObserver( self,selector:"batteryLevelChanged:",name:UIDeviceBatteryLevelDidChangeNotification,object:nil)
If your observer does not inherit from an Objective-C object,you must prefix your method with@objc
in order to use it as a selector.
@objcfuncbatteryLevelChanged(notification:NSNotification){ //dostuff }原文链接:https://www.f2er.com/swift/324294.html