Search code examples
iosswiftoptional-values

Double exclamation !! mark in Swift?


I know the definition for a single exclamation mark, but two?

I was coding today and the compiler "force" me to add one more ! to my sentence:

mySignal.subscribeNext({
        (value: AnyObject!) -> () in
        let list: RACSequence = value["myObject"]!!.rac_sequence
        ...

If I use only one ! mark, the project doesn't compile, giving me the error: "Value of optional type 'AnyObject?' not unwrapped; did you mean to use '!' or '?'?" Then I add one more ! and everything works.

What's the meaning for two exclamation marks in Swift?


Solution

  • You're storing an AnyObject! in the dictionary, so you're storing an optional as the value. Dictionary subscripts always return an optional of the value you're storing, so you're getting an optional optional, which is why you need two !, to unwrap it twice.