Re: NSObject vs id

Christiaan Hofman <[email protected]>
Newsgroups gmane.comp.macosx.devel
Message-ID <[email protected]>
On Sep 3, 2009, at 19:40, Bill Bumgarner wrote:

> On Sep 3, 2009, at 10:14 AM, Christiaan Hofman wrote:
>
>> The basic rule is that you can assume that all objects (type id)  
>> should at least respond to the basic memory management methods and - 
>> description (basically the Cocoa counterparts of CFBase.h).
>
> For practical purposes only, you can assume that.
>
> If I implement:
>
> @interface Rootonomous
> @end
>
> Instances of said class will not implement any methods.  Though rare  
> and typically limited purely to research purposes, there are reasons  
> to implement such a class (actually, it is an interesting thought  
> exercise to take the above and design your own object library w/your  
> own patterns of use).
>
> Such classes cannot be used in the context of the Foundation [unless  
> you were to implement <NSObject>, which kind of defeats the purpose  
> of the exercise), though, certainly and, thus, Christiaan's  
> statement is true in the context of Cocoa programming, for all  
> intents and purposes.
>

Well, yea, Apple's docs explicitly tell you to better not define your  
own root class, because you have some responsibility to implement the  
necessary methods.

>> For other methods you should first know they responds, usually  
>> using respondsToSelector: (or you know it from a context, e.g. when  
>> the object is a delegate or dataSource with required methods).
>
> Using respondsToSelector: outside of delegation or protocols with  
> optional methods is generally a sign that your design has gone in an  
> unfortunate direction.
>
> b.bum


Generally, but there are still exceptions. I also use it in some other  
cases, mostly for security reasons. Sometimes malicious users or  
program can hand edit a file on disc and change a value type in a  
dictionary (or preference), or AppleScript can return some unexpected  
type. In some cases you better be prepared for that, and check what  
you have. For instance, ANY object in an collection is only typed as  
id and satisfies no protocol, so if you did not directly created the  
collection yourself, it could in theory be anything. And I also use it  
in for backward-compatibility.

Christiaan
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.