Re: Simple Swift question
Re: Simple Swift question
- Subject: Re: Simple Swift question
- From: Quincey Morris <email@hidden>
- Date: Mon, 29 Jun 2015 22:41:30 +0000
On Jun 29, 2015, at 15:23 , Rick Mann <email@hidden> wrote:
>
> Oh, I think I figured it out. NSURL(string:) is optional, but NSURLRequest(URL:) can't. Very unexpected, and the error message I was getting did not clue me in.
>
> ---------
>
>
> How are you supposed to do simple things like this, succinctly?
>
> let url = NSURL(string: "<some url>")
> let req = NSURLRequest(URL: url)
> self.webView.loadRequest(req)
It’s not clear what the solution (or problem) was, since you seem to have left some words out. In general, though, the pattern for this sort of thing is:
> if let a = somethingReturningOptionalA (),
> let b = somethingReturningOptionalB (a),
> let c = somethingReturningOptionalC (b) {
> somethingUsingC (c)
> }
Note that each let clause can have an optional ‘where’ condition:
> if let a = somethingReturningOptionalA (),
> let b = somethingReturningOptionalB (a) where b.size > 1000000, …
and you can start with a purely boolean clause:
> if onePlusOne == Two,
> let a = somethingReturningOptionalA (), …
Alternatively, you can invert everything with ‘guard’ statements, picking off the nil values one by one, without any nesting, but that’s only appropriate when you can actually exit the scope if the guard fails:
> guard let a = somethingReturningOptionalA () else { break }
> guard let b = somethingReturningOptionalB (a) else {break }
> guard let c = somethingReturningOptionalC (b) else {break }
> somethingUsingC (c)
_______________________________________________
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