• 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: AnyObject and protocols
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: AnyObject and protocols


  • Subject: Re: AnyObject and protocols
  • From: Charles Srstka <email@hidden>
  • Date: Wed, 05 Aug 2015 19:18:36 -0500

I’m just on fire today, aren’t I? Posted this on the wrong thread. Sorry for the noise.

Charles

> On Aug 5, 2015, at 7:17 PM, Charles Srstka <email@hidden> wrote:
>
>> On Aug 4, 2015, at 6:23 PM, Rick Mann <email@hidden> wrote:
>>
>>> On Aug 4, 2015, at 16:22 , Quincey Morris <email@hidden> wrote:
>>>
>>> On Aug 4, 2015, at 15:58 , Rick Mann <email@hidden> wrote:
>>>>
>>>> I'm curious why, and what the right approach might be?
>>>
>>> As to why, the problem is that instances of a type conforming to a protocol aren’t necessarily objects. If you want them to be, try declaring your protocol like this:
>>>
>>> 	protocol P: class {
>>> 		…
>>> 	}
>>
>> Awesome, thanks!
>
> 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@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: 
 >AnyObject and protocols (From: Rick Mann <email@hidden>)
 >Re: AnyObject and protocols (From: Quincey Morris <email@hidden>)
 >Re: AnyObject and protocols (From: Rick Mann <email@hidden>)
 >Re: AnyObject and protocols (From: Charles Srstka <email@hidden>)

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