Swift 2: !, ? -” Value of optional type “…” not unwrapped”

obj?.fn() calls fn member function if the object isn’t null, otherwise doesn’t do anything.

obj!.fn() on the other hand asserts that obj isn’t null, and calls fn. If the object is null, you get an exception.

So it’s a difference in assertiveness: you either ask or simply claim the nullable property of a nullable object.

Leave a Comment