• 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: NSCopying is manual labor
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSCopying is manual labor


  • Subject: Re: NSCopying is manual labor
  • From: Ondra Cada <email@hidden>
  • Date: Fri, 1 Apr 2005 18:49:31 +0200

Adam,

On 31.3.2005, at 2:11, Adam wrote:

Now I also have a NSMutableArray of objects that I created. If I call mutableCopy on the array will it copy the contained objects as well if they have copyWithZone methods?

Of course not: mutableCopy is by definiton shallow (i.e, it never ever copies nested objects in containers).


Somewhat more tricky it is with copy, which *should* copy mutable nested objects (to get a real snapshot which nobody could change under your hands), but alas, when Core Foundation came, this behaviour was crippled, so it does not either (just to add insult to injury, the original proper behaviour is -- as of 10.3.8. -- still available in -[NSArray copy]; that implementation is though never used, unless you create your own NSArray subclass)

=== if you wanna test yourself like I have ===
#import <Foundation/Foundation.h>
@interface NSArray (ListIDs)
-(void)listIDs:(NSString*)what;
@end
@interface XArray:NSMutableArray {
NSMutableArray *contents; // does not matter how we store the data
}
+(XArray*)xarrayWithArray:(NSArray*)a;
@end
int main() {
[NSAutoreleasePool new];
id ma=[NSMutableArray arrayWithObjects:@"",[NSMutableString string],nil];
[ma listIDs:@"CoreFoundation-based original array"];
[[ma mutableCopy] listIDs:@"mutable copy"];
[[ma copy] listIDs:@"copy"];
id xa=[XArray xarrayWithArray:ma];
[xa listIDs:@"NSArray-based original array"];
[[xa mutableCopy] listIDs:@"mutable copy"];
[[xa copy] listIDs:@"copy"];
return 0;
}
@implementation NSArray (ListIDs)
-(void)listIDs:(NSString*)what {
NSMutableString *ms=[NSMutableString stringWithFormat:@"%@: %@ of ",what,[self class]];
for (id en=[self objectEnumerator],o;o=[en nextObject];)
[ms appendFormat:@"%@:%x ",[o class],o];
NSLog(ms); // valid, can't contain a %
}
@end
@implementation XArray
+(XArray*)xarrayWithArray:(NSArray*)a {
return [[[self alloc] initWithArray:a] autorelease];
}
-initWithArray:(NSArray*)a {
if (!(self=[super init])) return nil;
contents=[a mutableCopy];
return self;
}
// primitive methods:
-(unsigned)count {
return [contents count];
}
-objectAtIndex:(unsigned)n {
return [contents objectAtIndex:n];
}
-(void)addObject:o {
[contents addObject:o];
}
-(void)insertObject:o atIndex:(unsigned)n {
[contents insertObject:o atIndex:n];
}
-(void)removeLastObject {
[contents removeLastObject];
}
-(void)removeObjectAtIndex:(unsigned)n {
[contents removeObjectAtIndex:n];
}
-(void)replaceObjectAtIndex:(unsigned)n withObject:o {
[contents replaceObjectAtIndex:n withObject:o];
}
@end
---
Ondra Čada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: bindings, NSPopUpButtonCell, and images
  • Next by Date: Re: NSToolbar Problem
  • Previous by thread: bindings, NSPopUpButtonCell, and images
  • Next by thread: isa swizzling
  • Index(es):
    • Date
    • Thread