How do I create a mutable version of my object?
How do I create a mutable version of my object?
- Subject: How do I create a mutable version of my object?
- From: Jens Bauer <email@hidden>
- Date: Mon, 20 Jan 2003 08:24:40 +0100
Hi all,
I've been searching the net, both GNUStep, my mail and mamasam, but I
could not find specific information about how to make my object mutable.
I've collected a few guesses, and I might be wrong in some of the cases.
Say I have a few classes like these...
---8<-----8<-----8<-----
@interface MyExample
{
float exampleValue;
}
- (void)setExampleValue:(float)exampleValue;
- (float)exampleValue;
@interface MyExampleArray : NSObject <NSCopying, NSMutableCopying>
{
unsigned count;
}
- (float)firstExampleValue;
- (float)lastExampleValue;
@interface MyMutableExampleArray : MyExampleArray
{}
- (void)addExampleValue:(float)aValue;
--->8----->8----->8-----
...What do I have to make it possible to add items to the array then ?
-I got this far:
NSArray inherits from NSObject, NSMutableArray inherits from NSArray
NSString inherits from NSObject, NSMutableString inherits from NSString
...But that still doesn't make it clear to me, what's going on inside
NSArray and NSString.
Here's a guess:
NSArray contains a NSObject*.
NSArray contains an 'unsigned' value called 'count'.
NSMutableArray adds methods, that are able to change the # of objects
that NSArray points to, and if necessary, it'll also change the pointer.
As there are many ways to implement arrays, I can only guess, but does
anyone have suggestions on what to add to the above code, to make a
generic example on creating a mutable array ?
-I don't really want to subclass NSObject for the MyExample class
(which should be typedef'ed actually), as I bet NSObject uses some
extra bytes, and that would really be a waste.
Furthermore, I've seen the following code a few places:
- (id)copyWithZone:(NSZone *)zone
{
return(NSCopyObject(self, 0, zone));
}
- (id)mutableCopyWithZone:(NSZone *)zone
{
return(NSCopyObject(self, 0, zone));
}
// I've read somewhere, that I should invoke the superclass'
mutableCopyWithZone first. -But is there really a superclass here, and
if so, what should I return???
I've also seen this:
- (id)mutableCopyWithZone:(NSZone *)zone
{
return([[[self class] alloc] initWith
Data:[self data] length:[self
length]]);
}
...then copyWithZone just calls through to mutableCopyWithZone.
I appreciate any help I can get with this. :)
Love,
Jens
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.