swift内存泄露,与OC的混合

前端之家收集整理的这篇文章主要介绍了swift内存泄露,与OC的混合前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

猜你在找的Swift相关文章