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}总结
以上是内存溢出为你收集整理的swift内存泄露,与OC的混合全部内容,希望文章能够帮你解决swift内存泄露,与OC的混合所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)