> On Oct 14, 2019, at 10:44 PM, Laurent Daudelin via Cocoa-dev 
> <cocoa-dev@lists.apple.com> wrote:
> 
> Are people supposed to know instinctively when you unwrap with “?” and when 
> you do with “!”?

It’s quite simple; you nearly always unwrap with `?`.

The tiny minority of the time that `!` is needed is when you *know* something 
will never be nil, because a guarantee is being made somewhere else that the 
Swift compiler can’t see. Most of the time, in practice, this is because you’re 
dealing with data included with your own application. This plist key will 
*always* be set… because it’s in a plist that I included in the application, 
and I know what’s in it. This outlet will always be connected, because it’s in 
a nib that I included in the app, and I *know* that I’ve always connected it. 
If I forget to connect that outlet, it’s a *bug*, and the app *should* crash.

The other time `!` can be useful is when you’re not writing code that’s meant 
for production, when you’re prototyping or just screwing around in a playground 
or the debugger, and you don’t really care about correctness.

Seeing lots of `!` in production Swift is a code smell.

Charles

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to