iOS开发bug消灭之:Implicit use of 'self' in closure; use 'self.' to make capture semantics explicit

news/2024/7/20 21:35:47 标签: ios, swift

错误全文:

Implicit use of ‘self’ in closure; use ‘self.’ to make capture semantics explicit

swift版本30">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


http://www.niftyadmin.cn/n/1228951.html

相关文章

iOS开发bug消灭之:Could not cast value of type 'UINavigationController' to 'RedShirt.AddEmployeeTableVi...

swift版本:3.0 Xcode版本:8.0 错误全文: Could not cast value of type ‘UINavigationController’ to ‘RedShirt.AddEmployeeTableViewController’ 错误原因: 从AController segue 到BController的时候,由于B…

restore和recover的区别

restore 是还原物理文件 recover 是用日志恢复到一致 用了RMAN备份后就必须要用restore还原,然后才用recover恢复 restore——还原,与backup相对,从备份读出恢复备份的数据。 recover——恢复,把restore回来的数据经过一番处理变成…

iOS开发中有关Segue传输的流程研究

swift版本:3.0 Xcode版本:8.0 segue是ios开发中用来在不同的controller之间跳转和传输数据的核心方法,里面的一些常用的函数有prepare, shouldPerformSegue, performSegue等,但是他们之间的使用方法和调用顺序经常搞不清楚。针对…

C++11 中的高阶函数

看到了Swift里面有高阶函数map,filter,reduce。发现非常好用,以前写类似的东西要写好多循环。没怎么接触过函数式编程,在网上找了一下C的对应方法,分别为std::transform,std::remove_if,std::accumulate,总结如下: 首…

C++ 如何 int 转 string

写c的时候经常需要做int到string的转换。 下面是最简洁的方法 #include <string> std::string str std::to_string(35); to_string()函数不只能转换int&#xff0c;还可以转换各种float, double数据 c 中的定义如下&#xff1a; _LIBCPP_FUNC_VIS string to_strin…

程序员面试、算法研究、编程艺术、红黑树4大系列集锦与总结

程序员面试、算法研究、编程艺术、红黑树4大经典原创系列集锦与总结 作者&#xff1a;July--结构之法算法之道blog之博主。时间&#xff1a;2010年10月-2011年6月。出处&#xff1a;http://blog.csdn.net/v_JULY_v。声明&#xff1a;版权所有&#xff0c;侵犯必究。 前言 开博…

微信小程序开发踩坑指南

微信小程序开发踩坑指南 微信小程序很新&#xff0c;同时也意味着很多坑&#xff0c;特此记录&#xff0c;希望后来者少走弯路。 本文持续不定期更新&#xff08;最后更新2017年01月16日&#xff09; swiper无法显示&#xff0c;需指定宽高才可以正常显示 如果你参照微信官…

'physical corruption' 和'logical corruption' 的区别

physical corruption应该是指 磁盘介质的损坏logical corruption 是指当oracle读一个块时,做块头与块尾的校验,如果不一致,则出logical corruption