ios – 快速变量之前的下划线“_”是什么意思?

ios – 快速变量之前的下划线“_”是什么意思?,第1张

概述这是代码: var _selected: Bool = false { didSet(selected) { let animation = CATransition() animation.type = kCATransitionFade animation.duration = 0.15 self.label.layer. 这是代码:

var _selected: Bool = false {    dIDSet(selected) {        let animation = CATransition()        animation.type = kCATransitionFade        animation.duration = 0.15        self.label.layer.addAnimation(animation,forKey: "")        self.label.Font = self.selected ? self.highlightedFont : self.Font    }}

为什么变量是“_selected”而不是“selected”?

解决方法 它只是一种编码风格,不适用于Swift代码.

开发人员通常使用下划线标记事物,以表明它应该是私有的.

也就是说,下划线有实际用途_.阅读更多关于Local and External Parameter Names for Methods.

那么你如何避免使用_selected?现在,当您已经拥有所需的变量时,您有两个变量.

删除_将要求您覆盖成员变量(如何执行此 *** 作).

overrIDe var selected: Bool {    dIDSet {        println("Hello,\(selected)")    }}

此外,表视图单元格将具有可覆盖的方法setSelected(selected:animated),可能值得探索.

总结

以上是内存溢出为你收集整理的ios – 快速变量之前的下划线“_”是什么意思?全部内容,希望文章能够帮你解决ios – 快速变量之前的下划线“_”是什么意思?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1058603.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存