• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Need for Swift
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: Need for Swift
      • From: Alex Zavatone via Cocoa-dev <email@hidden>
References: 
 >Need for Swift (From: Kirk Kerekes via Cocoa-dev <email@hidden>)
 >Re: Need for Swift (From: Richard Charles via Cocoa-dev <email@hidden>)
 >Re: Need for Swift (From: Laurent Daudelin via Cocoa-dev <email@hidden>)
 >Re: Need for Swift (From: Pier Bover via Cocoa-dev <email@hidden>)
 >Re: Need for Swift (From: Charles Srstka via Cocoa-dev <email@hidden>)
 >Re: Need for Swift (From: Carl Hoefs via Cocoa-dev <email@hidden>)
 >Re: Need for Swift (From: Alex Zavatone via Cocoa-dev <email@hidden>)
 >Re: Need for Swift (From: Laurent Daudelin via Cocoa-dev <email@hidden>)

  • Prev by Date: Re: Need for Swift
  • Next by Date: Re: Need for Swift
  • Previous by thread: Re: Need for Swift
  • Next by thread: Re: Need for Swift
  • Index(es):
    • Date
    • Thread