Re: Creating Interface
Re: Creating Interface
- Subject: Re: Creating Interface
- From: Roland King <email@hidden>
- Date: Thu, 28 Aug 2008 21:56:00 +0800
I tried that before, making my protocol inherit from the NSObject
Protocol and it sort of worked, but not quite.
In my case I was using KVO and wanted to call
addObserver:forKeyPath:options:context:, something I'm very used to
NSObject just doing. However that's not part of the NSObject protocol,
it's part of the NSKeyValueObserving protocol so I thought, ok let me
inherit from that too, only to find that it's informal, there is no
defined .h file which has that so you can't do <NSObject,
NSKeyValueObserving>. I supposed I could have formalized it and just
written a protocol definition for it, but I didn't.
Instead I decided that what I really wanted was something which was an
NSObject with all the formal and informal protocols that implies, so I
just started using exactly that
-(void) foo:(NSObject<MyProtocol>*)theThingToDoItWith
and that worked just fine.
I don't quite understand why protocols like NSKeyValueObserving aren't
formalized and in a .h file somewhere so you could just use them in
the same way you can the NSObject protocol. They're documented, why
not just write the .h file. There's probably a very good reason for it
which has totally escaped me.
On Aug 28, 2008, at 9:03 PM, Phil wrote:
On Fri, Aug 29, 2008 at 12:32 AM, Jean-Daniel Dupas
<email@hidden> wrote:
When you use the id<protocol> syntaxt, the compiler assume your
object only
implements the method declared in your protocol.
If you want to use some other function, you have to use an object
type that
declare thoses functions, for example in your case, NSObject.
However, NSObject is both a class and a protocol. Although most
classes inherit from NSObject, this is not always the case (for
example, using DO, where objects inherit from the NSProxy root class).
It's preferrable if your protocol incorporates the NSObject protocol
as well, which enforces all implementors to also conform to the
NSObject protocol (and you take an id<MyProtocol>)---because you don't
actually care if you have an NSObject subclass, just that whatever
you've got behaves like an NSObject.
Phil
_______________________________________________
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
_______________________________________________
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