Re: Copying id
Re: Copying id
- Subject: Re: Copying id
- From: "Ken Ferry" <email@hidden>
- Date: Mon, 11 Aug 2008 20:22:38 -0700
On Mon, Aug 11, 2008 at 7:15 PM, Mike <email@hidden> wrote:
> Ken Thomases wrote:
>>
>> On Aug 10, 2008, at 2:05 AM, Mike wrote:
>>
>>> How does one go about copying an object specified by id when one doesn't
>>> know the object type and the object does not implement NSCopying protocol?
>>
>> The short answer is: you don't. If an object's class doesn't implement
>> NSCopying, then there are no known semantics for what making a copy of it
>> should mean. Just to pick a random example, what might it mean to copy an
>> NSTask object? Or an NSThread? I could keep going, of course.
>>
>> However, I'm having a hard time imagining under what circumstances you
>> would want to do this. If you don't know what kind of object you have, and
>> the object itself doesn't know how to make a copy, why do you want (or think
>> you need) to make a copy?
>>
>> Cheers,
>> Ken
>
> Because, I have a case where I need to create my own object for storing
> key/value pairs in a sorted array. Since NSDictionary, etc. doesn't allow
> sorted elements in the dictionary, I have to define my own - but in an array
> instead of a dictionary.
>
> Both the key and value for each key in the items in my array can be of any
> object type (id). The sorted key/value paid array class has to be able to
> make copies of the items in the arrays when it goes to sort them.
I was with you up until here. Why do you need to make copies?
You cannot have it both ways. Either you work on anything, or you
require copyable objects. It really doesn't make sense to copy an
arbitrary object.
However, I'm starting to wonder if your definition of 'copy' is not
syncing up with what we're thinking of..
Forgive me if this was already clear to you, but it's perfectly fine to say
NSString *foo = @"abc";
NSString *bar = foo;
Now foo and bar refer to the same object.
> Since the
> array class has to be generic to work with any kind of key, and any kind of
> object, I can't know the types beforehand. Since the array lass is generic
> and has to work for any objects, I can't assume that the objects implement
> NSCopying.
>
> Mike
> _______________________________________________
>
> 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