ios - 在 Swift 3 中将类与具有相同名称的实例成员区分开来

标签 ios swift3

在 Swift 3 中如何区分实例成员和同名的类成员?以前正常工作的现在会在 Xcode 8 beta 5 中产生错误:

"static member 'textColor' cannot be used on instance of type UITag"

public class UITag : UILabel {

    static var textColor = UIColor.white

   override public init(frame: CGRect) {
        super.init(frame: frame)

        textColor = UITag.textColor /* error: static member cannot be used on instance of type UITag */
        text      = " not set "
   }
}

最佳答案

这是一个奇怪的错误,我们可以讨论它是否是一个编译器错误,它实际上允许用静态变量隐藏非静态变量,但是请注意,具有相同的两个属性绝对是错误的代码name,一个是静态的,一个不是静态的,因为最后一个会遮挡前一个。 defaultTextColor 可能是一个更好的名字。

一个简单的解决方法是使用:

super.textColor = ...

关于ios - 在 Swift 3 中将类与具有相同名称的实例成员区分开来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39020146/

相关文章:

ios - iOS 的 NFC Tag Writer 示例或教程?

ios - 按 UIbutton 显示 UITextView 频繁

ios - 使用带数组的 NSDictionary 创建 JSON 文件

ios - 我怎样才能得到字典关键元素的区别?

swift - postman 和 alamofire 之间的响应时间

ios - 您可以使用与开发者帐户不同的帐户提交到应用商店吗

ios - cordova 文件传输插件在 ios 模拟器中不起作用

swift - UILabel 看起来有阴影像素化

ios - 约束动画不起作用

ios - 在 Swift 3 中永远调度时间