Why does SwiftyJSON create implicitly unwrapped optional for its constants?

210 views Asked by At

In SwiftlyJSON's code, it defines the following constants using forced unwrapping:

///Error code
public let ErrorUnsupportedType: Int! = 999
public let ErrorIndexOutOfBounds: Int! = 900
public let ErrorWrongType: Int! = 901
public let ErrorNotExist: Int! = 500

What's the purpose of declaring constants as implicitly unwrapped optional here?

Note: I am not asking why or when to use implicitly unwrapped, but rather why it's used in SwiftyJSON as I see no reason for that.

1

There are 1 answers

1
ilidar On

Well, maybe I'm wrong, of course better way is to ask author of the code. But here is my suggestions anyway:

  1. By blaming commit we can see that changes where made at Oct 6 2014 and as we know that Swift just released there could be some compiler warnings or maybe errors:

enter image description here

  1. Actually by writing Int! instead of Int we are forcing compiler to generate ImplicitlyUnwrappedOptional<Int> type (and by knowing this fact we could return to item 1):

    public let x: Int  = 1
    public let y: Int! = 2
    
    println(x.dynamicType)
    println(y.dynamicType)
    

    Outputs:

    Swift.Int
    Swift.ImplicitlyUnwrappedOptional<Swift.Int>