• 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 enums and NSNotificationCenter
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Swift enums and NSNotificationCenter


  • Subject: Re: Swift enums and NSNotificationCenter
  • From: Charles Srstka <email@hidden>
  • Date: Wed, 05 Aug 2015 18:38:21 -0500

On Aug 5, 2015, at 6:22 PM, Rick Mann <email@hidden> wrote:
>
> I'd like to be able to write code like this:
>
> enum MyNotifications : String {
>    case Note1		= "note1"
>    case Note2		= "note2"
> }
>
> let nc = NSNotificationCenter.defaultCenter()
> nc.postNotificationName(MyNotifications.Note1, nil)
>
> But Swift (2.0) doesn't let me do that. Is it possible to write an extension method to NSNotificationCenter that accepts "any enum derived from String" (or that's convertible to String, or something like that)? e.g.:
>
>    func postNotification(inName : enum:String)
>
> I tried doing this: http://pastebin.com/kXLEHu8f <http://pastebin.com/kXLEHu8f>
>
> But it gets multiple errors, as you can see.
>
> Is there an elegant way to do this? Seems counter-intuitive that I can't pass a derived enum in place of an ancestral type.

AFAIK there’s no way to do exactly what you’re asking for, but you could put a post() method on the enum itself.

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


  • Follow-Ups:
    • Re: Swift enums and NSNotificationCenter
      • From: Rick Mann <email@hidden>
References: 
 >Swift enums and NSNotificationCenter (From: Rick Mann <email@hidden>)

  • Prev by Date: Swift enums and NSNotificationCenter
  • Next by Date: Re: Swift enums and NSNotificationCenter
  • Previous by thread: Swift enums and NSNotificationCenter
  • Next by thread: Re: Swift enums and NSNotificationCenter
  • Index(es):
    • Date
    • Thread