iOS开发bug消灭之:Implicit use of 'self' in closure; use 'self.' to make capture semantics explicit
来源:互联网 发布:银行网络金融部 编辑:程序博客网 时间:2024/05/21 00:16
错误全文:
Implicit use of ‘self’ in closure; use ‘self.’ to make capture semantics explicit
swift版本:3.0
Xcode版本:8.0
错误原因:
在closure(闭包)内调用当前对象的属性或方法的时候,没有明确添加self指定
错误源码:
let editAction = UITableViewRowAction(style: .default, title: "编辑", handler: { (action,indexPath) -> Void in performSegue(withIdentifier: "ToEditSegue", sender: self.tableView.cellForRow(at: indexPath)) })
修正源码:
let editAction = UITableViewRowAction(style: .default, title: "编辑", handler: { (action,indexPath) -> Void in// 增加了self.关键字 self.performSegue(withIdentifier: "ToEditSegue", sender: self.tableView.cellForRow(at: indexPath)) })
关于为什么要在closure内添加self关键字:
由于closure是异步加载的,所以在closure内的代码真正的被调用的时候需要保证对象仍然存在,由于iOS的ARC机制,这里添加的self实际上等于给对象的引用数+1,在closure结束的时候在释放掉。确保了内存的管理,避免了内存泄漏。
参考&延伸阅读
http://katalisha.com/2016/01/22/ARC-Swift-closures-and-weak-self.html
0 0
- iOS开发bug消灭之:Implicit use of 'self' in closure; use 'self.' to make capture semantics explicit
- Use of 'self' in property access '...' before self.init initializes self
- use English to self-introduction or speech
- Regular Expression in C# - How to make use of Expresso
- iOS开发bug消灭之:Invalid update: invalid number of rows in section 0.
- iOS开发bug消灭之:Could not cast value of type 'UINavigationController' to 'RedShirt.AddEmployeeTableVi...
- Use of Explicit keyword
- [IOS] 错误:Cannot assign to 'self' outside of a method in the init family
- [ios]关于error:Cannot assign to 'self' outside of a method in the init family
- iOS 初始化报错 cannot assign to self outside of a method in the init
- Make Cool Use of Windows Remote Desktop In XP
- How to use NSNotification in iOS?
- NDoc: How to Make Good Use of Your XML Comments?
- NDoc: How to Make Good Use of Your XML Comments?
- NDoc: How to Make Good Use of Your XML Comments?
- how to write your annotation types and make use of built-in annotations to control their behavior
- iOS开发bug消灭之:Your application has presented a UIAlertController of style ...
- how to make Completion Handlers and use it in Swift
- iOS国际化-多语言适配
- Android开发之异步操作(一)
- Redis集群搭建方法以及脚本搭建
- GreenDao使用
- Maze (概率DP)
- iOS开发bug消灭之:Implicit use of 'self' in closure; use 'self.' to make capture semantics explicit
- 37. Sudoku Solver
- memcache具体使用
- java io系列11之 FilterOutputStream
- 牛客网刷题之变态跳台阶
- Java Math.log10()方法
- 43. Multiply Strings
- 64. Minimum Path Sum
- codeforces 505C Mr. Kitayuta, the Treasure Hunter( dp 缩减规模 )