• 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: Swift 2 init() with CF types and throws
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Swift 2 init() with CF types and throws


  • Subject: Re: Swift 2 init() with CF types and throws
  • From: Roland King <email@hidden>
  • Date: Thu, 02 Jul 2015 08:06:06 +0800

graphicsPort doesn’t return an Optional however currentContext does, so cocoaCTX is an NSGraphicsContext? which is what the error message was trying to tell you.

let cocoaCTX = NSGraphicsContext.currentContext()
let sysCTX = cocoaCTX?.graphicsPort as! CGContextRef?

is what you need to get an optional CGContextRef out of it (and it took me 3 shots to get that one right). ‘?’ after the cocoaCTX (which autocomplete adds for you), a force downcast but to a CGContextRef? else you’ll go bang if the NSGraphicsContext is nil

As Marco points out, that won’t work in your initializer, quite.

> On 2 Jul 2015, at 07:46, Rick Mann <email@hidden> wrote:
>
> I think I figured this out. graphicsPort does not return an Optional type. I guess the only way to know that is via Xcode's Code Completion, is that right? Oh, the docs seem to show it, too (assuming it can find either).
>
> Sorry for the noise.
>
>> On Jul 1, 2015, at 16:28 , Rick Mann <email@hidden> wrote:
>>
>> I'm trying to do this:
>>
>> class
>> Context
>> {
>>   init()
>>       throws
>>   {
>>       let cocoaCTX = NSGraphicsContext.currentContext()
>>       guard let sysCTX = cocoaCTX.graphicsPort as! CGContextRef else { throw Errors.InvalidContext }
>>       CGContext = sysCTX;
>>   }
>>
>>   var CGContext : CGContextRef
>> }
>>
>>
>> But I'm getting
>>
>> error: value of optional type 'NSGraphicsContext?' not unwrapped; did you mean to use '!' or '?'?
>>               guard let sysCTX = cocoaCTX.graphicsPort as! CGContextRef else { throw Errors.InvalidContext }
>>                                  ^
>>                                          !
>> In the typical if-let, you don't add the ?. If I do add the ?, I get:
>>
>> error: '_??' is not convertible to '_??'
>>               guard let sysCTX = cocoaCTX.graphicsPort? as! CGContextRef else { throw Errors.InvalidContext }
>>                                  ~~~~~~~~~~~~~~~~~~~~~^
>>
>> Errors is an enum I created in a different file.
>>
>> I always seem to trip up on the conditional optional unwrapping. Not sure what I'm doing wrong here. Help is much appreciated. Thanks!
>>
>> --
>> Rick Mann
>> email@hidden
>>
>>
>>
>> _______________________________________________
>>
>> 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
>
>
> --
> Rick Mann
> email@hidden
>
>
>
> _______________________________________________
>
> 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


_______________________________________________

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


References: 
 >Swift 2 init() with CF types and throws (From: Rick Mann <email@hidden>)
 >Re: Swift 2 init() with CF types and throws (From: Rick Mann <email@hidden>)

  • Prev by Date: Re: Swift 2 throws from init()
  • Next by Date: Re: Swift 2 throws from init()
  • Previous by thread: Re: Swift 2 init() with CF types and throws
  • Next by thread: Re: Swift 2 init() with CF types and throws
  • Index(es):
    • Date
    • Thread