Re: Can I show different content in different screens? (screensaver)
Re: Can I show different content in different screens? (screensaver)
- Subject: Re: Can I show different content in different screens? (screensaver)
- From: Roland King <email@hidden>
- Date: Sat, 25 Jul 2015 10:19:00 +0800
> On 25 Jul 2015, at 01:13, Juanjo Conti <email@hidden> wrote:
>
> I wonder why the second line doesn't' compile
>
> var screens = NSScreen.screens() as! [NSScreen]
>
> find(screens, window?.screen!)
>
> On Fri, Jul 24, 2015 at 1:19 PM, Juanjo Conti <email@hidden>
> wrote:
The error message should tell you - but if it doesn’t make sense and you can’t work it out from the definitions then do the usual swift trick and break it down
let screen = window?.screen!
then option-click screen and see what type it is, it’s an NSScreen?. Find takes an array of X and an X to look up in it. You are giving it an array of X and and X? to look up in it so it doesn’t work.
Why is it an NSScreen? when you put the ! at the end? Because your window is an NSWindow? and an NSWindow’s screen property is an NSScreen?. So what window?.screen! actually means is, if window isn’t nil get its screen property and implicitly unwrap it to an NSScreen. But if window is nil, it stops there and returns nil, so the return is either nil or an NSScreen hence it’s an NSScreen?. If you leave the ‘!’ out at the end the return type is still .. exactly the same .. if window isn’t nil and its screen isn’t either, return an NSScreen, else return nil, so still an NSScreen?
if you wanted to implicitly unwrap the screen you needed
let screen = ( window?.screen)!
I’ve pretty much stopped writing code like that because it’s a runtime error waiting to happen, I would write
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.
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
_______________________________________________
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