Stéphane de Luca Stéphane de Luca - 1 year ago 111
iOS Question

Differentiating class from instance member with same name in Swift 3

How do you differentiate an instance member from a class member having the same name in Swift 3? What's usually working before now produces an error in 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 "

Answer Source

This is a strange error and we could discuss whether it is a compiler bug that it is actually allowed to shadow a non-static variable with a static variable, however note that it is definitely bad code to have two properties with the same name, one static and one not static, because the last one will shadow the previous one. Probably defaultTextColor would be a better name.

A simple workaround is to use:

super.textColor = ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download