4

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.

Boon
  • 40,656
  • 60
  • 209
  • 315
  • possible duplicate of [Why create "Implicitly Unwrapped Optionals"?](http://stackoverflow.com/questions/24006975/why-create-implicitly-unwrapped-optionals) – Shripada Jun 13 '15 at 03:47
  • Not a duplicate - I have seen this post before asking this. I am asking why the constant needs to be declared this way in SwiftyJSON. – Boon Jun 13 '15 at 10:13
  • I'm voting to close this as primarily opinion based. While user might be capable of adding commentary about why or why not you should do this in the general case, you're asking about a specific case from a specific library. Unless the SwiftyJSON author's stop by and offer a hand-of-God answer, any other answer can be nothing but pure conjecture. – nhgrif Jun 13 '15 at 18:14
  • Learning from other's code is a legitimate activity of coding and learning, and asking a question on why certain things are done in a certain way is valuable. Over-zealous generalization that any question of this nature can only generate answer of pure conjecture is not only incorrect (and who is to say conjecture has no value?), it dissuades good questions from being asked. I urge you to re-examine your belief and the damage it will do to SO community. – Boon Jun 13 '15 at 22:31

1 Answers1

0

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>
    
ilidar
  • 81
  • 5
  • Thanks for investigating - what kind of compiler warning do you think necessitates this. – Boon Jul 17 '15 at 15:07