> On Aug 5, 2015, at 7:00 PM, Rick Mann <[email protected]> wrote:
> 
>> 
>> On Aug 5, 2015, at 16:57 , Charles Srstka <[email protected] 
>> <mailto:[email protected]>> 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

_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to