Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing StringLiteralConvertible on NSURL

Tags:

swift

I've implemented StringLiteralConvertible, which extends ExtendedGraphemeClusterLiteralConvertible. It looks like it wants me to implement that too. When I do, however, Xcode says it doesn't know what ExtendedGraphemeClusterLiteralType is. I'm not sure what it wants from me...

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"
like image 628
Jarsen Avatar asked Jun 07 '14 20:06

Jarsen


1 Answers

The problem is related to your extension not conforming to the protocol. If you CMD+Click on the StringLiteralConvertible protocol, to follow it to it's definition, you will find that the typealias StringLiteralType and typealias ExtendedGraphemeClusterLiteralType are set to String.

That being said, you should modify your extension to the following:

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()
   }
}

Information about typealias is described in "The Swift Programming Language" book from pages 606-609, under the section Associated Types.

like image 108
wbennett Avatar answered Oct 02 '22 23:10

wbennett