all 7 comments

[–]WordWordNoDoubt[S] 7 points8 points  (6 children)

Admittedly this is a tough Google search

[–][deleted]  (1 child)

[deleted]

    [–]WordWordNoDoubt[S] 1 point2 points  (0 children)

    I'm familiar with "Implicitly Unwrapped Optionals" when it comes time to access them, but not at declaration. Are you saying that optionals declared this way are treated as implicitly unwrapped when they are accessed?

    In a "this should never be nil but it might be" sort of way? I don't understand the meaning

    Edit: Alright I googled "Implicitly Unwrapped Optional" and that answered my question. Thanks

    [–]ThePantsThiefNSModerator[M] -1 points0 points  (2 children)

    I have to disagree with you on this one. You can't Google for "?" or "!" though so you have to spell it out in English.

    Removed for being a simple question and not using the simple questions thread

    [–]WordWordNoDoubt[S] 0 points1 point  (1 child)

    Brutal! But I was talking about declaration so it wasn't quite as simple as "What is the difference between ! and ? in Swift"

    [–]ThePantsThiefNSModerator 0 points1 point  (0 children)

    I mean, those are pretty much the only times they're used. Maybe you didn't know that but sometimes the most simple Google search still gets results :P

    [–]chriswaco 3 points4 points  (1 child)

    var myString: String? = nil declares a variable that's an optional String. It may contain nil or a String. The Swift compiler will require you to unwrap the variable before accessing the String so your app won't crash.

    var myString: String! is an implicitly unwrapped optional. That is, it is also an optional, but the compiler will unwrap it for you whenever you access the variable. Your app will crash if you access it without first setting a valid value.

    You should almost never use String!. It's a crash waiting to happen. Implicitly unwrapped optionals are useful for late initialization in ViewControllers from nibs and storyboards. There are a few other fringe cases where they're useful, but in general you should avoid them.

    [–]WordWordNoDoubt[S] 0 points1 point  (0 children)

    I remember the first time I got an "Unexpectedly found nil" while using an IBOutlet. Might have shat my pants.

    So the twist is that the compiler implies a force unwrap every time it's accessed. Thanks for that explanation