Re: Determining if NSArray/NSDictionary is immutable
Re: Determining if NSArray/NSDictionary is immutable
- Subject: Re: Determining if NSArray/NSDictionary is immutable
- From: p3consulting <email@hidden>
- Date: Sun, 10 Jul 2005 10:06:49 +0200
Indeed if you look at a classdump of Foundation, NSCFDictionary is a
subclass of NSMutableDictionary...
A solution maybe to use [[dict classForCoder] description]
that returns correctly NSDictionary for non-mutable ones and
NSMutableDictionary for mutables ones.
#import <Foundation/Foundation.h>
int main(int argc, char **argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]
init] ;
NSDictionary *d = [NSDictionary dictionary] ;
NSLog(@"d is mutable %d",[d isKindOfClass:[NSMutableDictionary
class]]) ;
NSLog(@"d %@",[d class]);
NSLog(@"classForCoder %@",[d classForCoder]) ;
d = [NSMutableDictionary dictionary] ;
NSLog(@"d is mutable %d",[d isKindOfClass:[NSMutableDictionary
class]]) ;
NSLog(@"d %@",[d class]);
NSLog(@"classForCoder %@",[d classForCoder]) ;
[pool release] ;
return 0 ;
}
2005-07-10 10:00:52.239 testImmutable[4285] d is mutable 1
2005-07-10 10:00:52.243 testImmutable[4285] d NSCFDictionary
2005-07-10 10:00:52.245 testImmutable[4285] classForCoder NSDictionary
2005-07-10 10:00:52.247 testImmutable[4285] d is mutable 1
2005-07-10 10:00:52.249 testImmutable[4285] d NSCFDictionary
2005-07-10 10:00:52.250 testImmutable[4285] classForCoder
NSMutableDictionary
Pascal Pochet
email@hidden
----------------------------------
PGP
KeyID: 0x208C5DBF
Fingerprint: 9BFB 245C 5BFE 7F1D 64B7 C473 ABB3 4E83 208C 5DBF
Le 10 juil., 2005, à 04:18, email@hidden a écrit :
Given that you are writing a test harness, can you safely attempt to
modify the returned collection and catch the exception if it is
immutable?
I.e.
@try {
[returnArray addObject; 0];
} @catch {id e} {
/// nope, not mutable
return;
}
// hey, we mutated... boo.
Well, yes, that's what I've done for the moment, as I said in my
original post. It works well enough, except it requires enabling ObjC
exceptions. No real biggy, but I do a lot of framework and library
development, and hate solutions which complicate the build process,
since that makes using my stuff harder for others.
Wade Tregaskis (AIM, Yahoo & Skype: wadetregaskis, AIM audio/video
chat: email@hidden, ICQ: 40056898, MSN:
email@hidden, Jabber: email@hidden)
-- Sed quis custodiet ipsos custodes?
_______________________________________________
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
_______________________________________________
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