• 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: Simple Swift question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Simple Swift question


  • Subject: Re: Simple Swift question
  • From: Jens Alfke <email@hidden>
  • Date: Mon, 29 Jun 2015 15:35:06 -0700

> On Jun 29, 2015, at 3:23 PM, Rick Mann <email@hidden> wrote:
>
> Oh, I think I figured it out. NSURL(string:) is optional, but NSURLRequest(URL:) can't. Very unexpected

Creating an NSURL from a string can fail, since not all strings are valid URLs.
But creating an NSURLRequest isn’t failable; you always get an instance.

> But it gets rapidly ridiculous with a bunch of nested if-lets

To be fair, this would have occurred in Objective-C too, assuming you were properly testing for nil everywhere. Swift just doesn’t let you get away with closing your eyes and pretending that [NSURL URLWithString:] can’t return nil. (If “<some url>” represents a constant string that you happen to know is a valid URL, then you can just put an “!” after it to avoid the optional. It’s perfectly appropriate in this place; it’s the moral equivalent of adding an NSAssert that the NSURL isn’t nil.)

The unsightly nesting of the if-lets can be avoided with the nifty new ‘guard’ statement in Swift 2.

—Jens
_______________________________________________

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: Simple Swift question
      • From: Rick Mann <email@hidden>
References: 
 >Simple Swift question (From: Rick Mann <email@hidden>)

  • Prev by Date: Simple Swift question
  • Next by Date: Re: Simple Swift question
  • Previous by thread: Simple Swift question
  • Next by thread: Re: Simple Swift question
  • Index(es):
    • Date
    • Thread