Re: supportedInterfaceOrientations in Swift
Re: supportedInterfaceOrientations in Swift
- Subject: Re: supportedInterfaceOrientations in Swift
- From: Greg Parker <email@hidden>
- Date: Thu, 19 Jun 2014 14:04:12 -0700
> On Jun 19, 2014, at 2:23 AM, Roland King <email@hidden> wrote:
>
> I'm overriding supportedInterfaceOrientations in my view controller because I want it to return Portrait + PortraitUpsideDown, and on iPhone PortraitUpsideDown is not included in the standard return.
>
> The Objective-C method would look something like this
>
> -(NSUInteger)supportedInterfaceOrientations
> {
> return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
> }
>
> Swift has stubbed out the method thus
>
> override func supportedInterfaceOrientations() -> Int
> {
> }
>
> which makes some sense. I'm tripping over myself trying however to return the correct Int without casting my casts to casts of casts.
Please file a bug report. -supportedInterfaceOrientations should be declared to return UIInterfaceOrientationMask, not Int. This sort of loose typing works in C but not in Swift.
> UIInterfaceOrientationMask is a struct with a number of Type properties, so you can write
>
> UIInterfaceOrientationMask.Portrait
>
> to get its value however that's a struct so you need to ask for the 'value' property, which is defined to return a UInt and seems to do the same as toRaw(). Trying to return UIInterfaceOrientationMask.Portrait.value however gives the error
>
> 'NSNumber is not a subtype of Int'
>
> Odd as I thought UInt was a basic type like Int, but clearly it's an NSNumber.
UInt is just as basic as Int and is unrelated to NSNumber. The error message is incorrect (it's a known bug). I think the problem was that .value returns UInt and your method returns Int.
> A bit of luck with autocompletion threw up the asSigned() method, which seems to do the same as constructing an Int using Int( .. )
>
> That appears to work but the final line is now
>
> return UIInterfaceOrientationMask.Portrait.value.asSigned() + UIInterfaceOrientationMask.PortraitUpsideDown.value.asSigned()
>
> Surely there's something a little less unwieldy, anyone have something? Or is this just a case where Swift's strong typing meets Cocoa's C background and ends up in knots?
>
> Roland
>
> PS in the course of trying things out I tried various ways of initializing a UInt and failed dismally often with evil stack traces in the console.
>
> var a : Int = 123
> var b : UInt = 123 // fail
> var c : UInt = a // fail
> var d : UInt = UInt( a ) // fail
The last should work assuming `a` is not negative.
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
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