• 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: referencing a Swift Dictionary from Objective-C
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: referencing a Swift Dictionary from Objective-C


  • Subject: Re: referencing a Swift Dictionary from Objective-C
  • From: Gavin Eadie <email@hidden>
  • Date: Sun, 11 Sep 2016 10:27:28 -0400

> On Sep 11, 2016, at 1:31 AM, Charles Srstka <email@hidden> wrote:
>
>> On Sep 11, 2016, at 12:16 AM, Gavin Eadie <email@hidden> wrote:
>>
>> I’m moving some code from Obj-C to Swift and, from time to time, I open a gap I cannot see across.  This is one, and I’d love some assistance.
>>
>> I converted a pile of utility Obj-C code that included a class method of the form on the rhs of:
>>
>>  xxx = [UIColor colorFromName:@"aliceblue"]
>>
>> In the new Swift replacement of the utility code, I added a global constant Dictionary of the form:
>>
>>  public let colorLookup = [
>>     "aliceblue" : UIColor.init(colorLiteralRed:0.0, green:0.5, blue:1.0, alpha:1.0),
>>     …
>>  ]
>>
>> and changed the Obj-C call to
>>
>>  xxx = colorLookup[@"aliceblue"]
>>
>>
>> The Obj-C compiler complains that the subscript is not numeric.  It also appears to not be able to resolve the global constant "colorLookup" (which probably explains the error in the previous sentence).  I notice that "colorLookup" doesn’t appear in "Utilities-Swift.h" ..
>>
>> There are other (and better) ways I could code this, but I’m curious why "colorLookup" is invisible to Objective-C .. Thanks, Gavin
>
> Wrap the variable in an Objective-C class and it should be fine:
>
> public class Constants: NSObject {
> 	public let colorLookup: [String : UIColor] = ...
> }
>
> Charles

.. nearly!  We just need "static" on the property:

    class Constants: NSObject {
        static let colorLookup: [String : UIColor] = ...
    }

.. thanks, Charles
_______________________________________________

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: 
 >referencing a Swift Dictionary from Objective-C (From: Gavin Eadie <email@hidden>)
 >Re: referencing a Swift Dictionary from Objective-C (From: Charles Srstka <email@hidden>)

  • Prev by Date: Re: Progress Indicator
  • Next by Date: Best method for showing & hiding an inspector nspanel
  • Previous by thread: Re: referencing a Swift Dictionary from Objective-C
  • Next by thread: Best method for showing & hiding an inspector nspanel
  • Index(es):
    • Date
    • Thread