[已解决]Swift代码调用类的static变量出错:Static member cannot be used on instance of type

代码:

class ChatViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, MessageDelegate {

//  var chatWithUser: String = "teste03@local"
  static var chatWithUser: String = "teste03@local"
出错:
Static member ‘chatWithUser’ cannot be used on instance of type ‘ChatViewController’
Static member chatWithUser cannot be used on instance of type ChatViewController
搜:
swift Static member cannot be used on instance of type
参考:
message.addAttributeWithName("to", stringValue: ChatViewController.chatWithUser as String)
即可。
[总结]
Swift中,如果是一个SomeClass的static变量someStaticValue,则调用时,不能直接使用someStaticValue,而要写成SomeClass.someStaticValue的形式才可以。
即便是在当前自己的类里面,也要写成SomeClass.someStaticValue才可以。
实例:
class ChatViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, MessageDelegate {
    static var chatWithUser: String = "teste03@local"

  @IBAction func sendMessage() {
    ...
        message.addAttributeWithName("to", stringValue: ChatViewController.chatWithUser as String)
      ...
    }
  }
}
供参考。


发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量