Re: Need for Swift
Re: Need for Swift
- Subject: Re: Need for Swift
- From: Charles Srstka via Cocoa-dev <email@hidden>
- Date: Tue, 15 Oct 2019 18:08:07 -0500
> On Oct 14, 2019, at 10:44 PM, Laurent Daudelin via Cocoa-dev
> <email@hidden> 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 (email@hidden)
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:
This email sent to email@hidden