> On Jul 24, 2015, at 7:19 PM, Roland King <r...@rols.org> wrote:
> 
> if let screen = window?.screen 
> {
>       find(…)
> }
> else
> {
>       // handle the lack of a screen gracefully
> }
> 
> it’s longer, but it’s clearer and it says I’ve considered what semantically 
> it means to have a nil screen. 

You may prefer the new `guard let` construct for this pattern.


> PS does anyone know (in swift 2.0) of a way to use the ternery operator with 
> let/case so you can initialise a variable without curly braces everywhere, I 
> want to write
> 
> let myVar = ( let ifNotOptional = something?.something else ) ? 
> ifNotOptional.stringName : “No Name”
> 
> but can’t find any syntax to do something like that

You can separate the let declaration from the initialization, as long as the 
compiler can see that it is assigned exactly once before it is used along all 
code paths. The compiler recently became smarter at this analysis so it works 
in more cases. (Downside: you must specify the variable's type.)

    let myVar: String
    if let value = something?.somethingElse {
        myVar = value.stringName
    } else {
        myVar = "No Name"
    }


-- 
Greg Parker     gpar...@apple.com     Runtime Wrangler



_______________________________________________

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