在NSURL上实现StringLiteralConvertible

时间:2014-06-07 20:37:38

标签: swift

我已实施StringLiteralConvertible,其范围为ExtendedGraphemeClusterLiteralConvertible。看起来它也希望我实现它。然而,当我这样做时,Xcode表示它不知道ExtendedGraphemeClusterLiteralType是什么。我不确定它对我有什么要求......

extension NSURL : StringLiteralConvertible {
    class func convertFromStringLiteral(value: StringLiteralType) -> Self {
        return self(string: value)
    }

    class func convertFromExtendedGraphemeClusterLiteral(value: ExtendedGraphemeClusterLiteralType) -> Self {
        // Use of undeclared type ExtendedGraphemeClusterLiteralType :( ?
    }
}

let url : NSURL = "http://apple.com"

1 个答案:

答案 0 :(得分:2)

问题与您的扩展程序不符合协议有关。如果你CMD +点击StringLiteralConvertible协议,按照它的定义,你会发现typealias StringLiteralType和typealias ExtendedGraphemeClusterLiteralType被设置为String。

话虽如此,您应该将您的扩展名修改为以下内容:

extension NSURL : StringLiteralConvertible {

   class func convertFromStringLiteral(value: String) -> Self
   {
              //do what you were going to do
      return self()
   }

   class func convertFromExtendedGraphemeClusterLiteral(value: String) -> Self{
              //do what you were going to do
      return self()
   }
}

有关类型的信息,请参阅第606-609页的“The Swift Programming Language”一书,关联类型部分。