【问题标题】:What is the namespace of Swift's type(of:) function?Swift 的 type(of:) 函数的命名空间是什么?
【发布时间】:2018-04-11 19:58:14
【问题描述】:

我有以下 Swift 类:

class Foo: CustomDebugStringConvertible {
  let type: Int = 0
  var debugDescription: String {
    return "<\(type(of:self)) type: \(type)>"
  }
}

Xcode 9.2 抱怨:

Cannot call value of non-function type 'Int'

我想这是因为我的本地 type 与 Swift 的全局 type 冲突。什么是 Swift 全局命名空间,以便我可以消除歧义?

【问题讨论】:

  • Swift 不是 Objective-C,除非你有很好的理由,不要继承 NSObject 并且不要标记你的类 @objc。即使你有理由在真实课堂上这样做,MCVE 也绝对不需要这些。
  • 我不太确定为什么这个问题被否决了。这对我来说似乎很清楚。此外,@DavidPasztor OP 并没有询问有关 ObjC 的问题。他在询问type(of:) 函数。
  • @daltonclaybrook 我知道这一点,但如果问题中发布的代码有问题,即使该特定问题与所讨论的问题无关,每个人都最好指出这一点...
  • 我把这变成了一个纯粹的快速问题。也许我会得到一些支持。

标签: swift namespaces


【解决方案1】:

Swift 是 Swift 全局命名空间名称,所以我的类应该是:

class Foo: CustomDebugStringConvertible {
  let type: Int = 0
  var debugDescription: String {
    return "<\(Swift.type(of:self)) type: \(type)>"
  }
}

【讨论】:

    猜你喜欢
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 2015-12-19
    相关资源
    最近更新 更多