Re: Convenience Methods
Re: Convenience Methods
- Subject: Re: Convenience Methods
- From: Erik Buck <email@hidden>
- Date: Wed, 26 Sep 2007 21:48:24 -0400
On Sep 26, 2007, at 8:40 PM, Wade Tregaskis wrote:
They don't get any benefits from immutable classes because they
aren't really immutable.
This has been said a few times, but I can't think of any examples
where you can mutate a NSArray, or NSString, or other such
immutable classes. ?
Wade
You're right! However, you might find this test program slightly
amusing:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Find out if CFArray (which is really NSCFArray) responds to -
addObject:
CFArrayRef testArray1 = CFArrayCreate(NULL, NULL, 0, NULL);
if([(id)testArray1 respondsToSelector:@selector(addObject:)])
{
NSLog(@"%@ respondsToSelector:@selector(addObject:)", [(id)
testArray1 class]);
}
// Find out if NSArray (which is really NSCFArray) responds to -
addObject:
NSArray *testArray2 = [NSArray array];
if([testArray2 respondsToSelector:@selector(addObject:)])
{
NSLog(@"%@ respondsToSelector:@selector(addObject:)",
[testArray2 class]);
}
// Find out if NSMutableArray (which is really NSCFArray)
responds to -addObject:
NSArray *testArray3 = [NSMutableArray array];
if([testArray3 respondsToSelector:@selector(addObject:)])
{
NSLog(@"%@ respondsToSelector:@selector(addObject:)",
[testArray3 class]);
}
// Find out if copying an "immutable" array returns the sameobject
NSLog(@"Is %@ the same as its copy: %@ ?", [(id)testArray1
class], ((id)testArray1 == [[(id)testArray1 copy] autorelease]) ?
@"YES" : @"NO");
NSLog(@"Is %@ the same as its copy: %@ ?", [testArray2 class],
(testArray2 == [[testArray2 copy] autorelease]) ? @"YES" : @"NO");
NSLog(@"Is %@ the same as its copy: %@ ?", [testArray3 class],
(testArray3 == [[testArray3 copy] autorelease]) ? @"YES" : @"NO");
CFRelease(testArray1);
[pool release];
return 0;
}
Essentially, as an implementation detail, NSCFArray stores
information about whether it is immutable or not. If it thinks it's
immutable then it raises an exception if you try to mutate it, but it
doesn't bother to respond NO to -respondsToSelector:@selector
(addObject:).
_______________________________________________
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