• 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: Rick Mann <email@hidden>
  • Date: Wed, 05 Aug 2015 17:21:52 -0700

Thanks, I'll see what I can make of that. Still not quite as nice as being able to pass an enum to an NSNotificationCenter extension method, but partway there.

Would it make sense for Swift to just let you add method implementations to protocols (perhaps as a shortcut to the protocol-extension technique you used)?

> On Aug 5, 2015, at 17:18 , Charles Srstka <email@hidden> wrote:
>
>> On Aug 5, 2015, at 7:00 PM, Rick Mann <email@hidden> wrote:
>>
>>>
>>> On Aug 5, 2015, at 16:57 , Charles Srstka <email@hidden> wrote:
>>>
>>> I get that error any time I try to subclass *any* enum. I don’t think enums are supposed to be subclassable.
>>
>> Guess I'll submit a bug. Seems perfectly reasonable to do.
>
> Sorry for the delay; it took me a while to figure out why the line of code with addObserverForName was giving wonderfully helpful errors like “‘String’ is not convertible to ‘StringLiteralConvertible’” (grumble grumble).
>
> Anyway, what you want may be possible via a protocol extension:
>
> import Foundation
>
> protocol PostableEnum {
>    func post()
>    var rawValue: String { get }
> }
>
> extension PostableEnum {
>    func post() {
>        NSNotificationCenter.defaultCenter().postNotificationName(self.rawValue, object: nil)
>    }
> }
>
> enum SomeEnum : String, PostableEnum {
>    case Foo = "Foo"
> }
>
> let someEnum = SomeEnum.Foo
>
> let foo = NSObject()
>
> let observer = NSNotificationCenter.defaultCenter().addObserverForName("Foo", object: nil, queue: nil) { _ in
>    print("notification received")
> }
>
> someEnum.post()
>
> CFRunLoopRun()
>
> Charles
>


--
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


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

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