Re: NSString coding errors
Re: NSString coding errors
- Subject: Re: NSString coding errors
- From: Nir Soffer <email@hidden>
- Date: Tue, 16 Jan 2007 15:37:03 +0200
On Jan 16, 2007, at 00:53, Adam Radestock wrote:
Hi everyone,
I've just added an NSString object to one of my custom objects,
which conforms to the NSCoding Protocol, but am getting errors
whenever I try and encode the NSString object.
Below are my .h and .m files for the custom class, can anyone point
out the error in my code? I can't see the wood for the trees...
Here are some woods the pop out:
@implementation SCRPatch
- (id) init
{
if (self = [super init])
{
NSArray * keys = [NSArray arrayWithObjects:
@"patchName", @"patchPath", @"patchIcon", @"isPlaying",
@"duration", nil];
NSArray * values = [NSArray arrayWithObjects: [NSString
string], [NSString string], [NSNull null], [NSNull null], [NSNull
null], nil];
properties = [[NSMutableDictionary alloc] initWithObjects:
values forKeys: keys];
A much cleaner way to do this is:
properties = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"", @"patchName",
@"", @"patchPath",
[NSNull null], @"patchIcon",
[NSNumber numberWithBool:NO], @"isPlaying", nil];
Note that isPlaying encode a boolean value.
qcPerameters = [[NSMutableDictionary alloc] init];
patchTypeString = [NSString string];
Should be:
[self setPatchTypeString:@""];
- (id)copyWithZone:(NSZone *)zone {
return [self retain];
}
This is ok only if your object is immutable - is it?
If it is, why are you using mutable dictionaries? It means also that
you can allow public setter methods like setPatchTypeString:.
- (void) dealloc
{
[properties release];
[qcPerameters release];
[patchTypeString release];
[super dealloc];
}
- (NSMutableDictionary *)properties
{
return properties;
}
This means that clients can change the state of the object! Do you
really want to do this?
This is also bad because to extract data from the dictionary, clients
need to know the name of the keys. Either you have duplicate code or
you expose the implementation.
- (NSMutableDictionary *)qcPerameters
{
return qcPerameters;
}
Same problem.
- (void)setPatchTypeString:(NSString *)newPatchTypeString
{
if (patchTypeString != newPatchTypeString)
{
[patchTypeString autorelease];
patchTypeString = [NSString stringWithString:newPatchTypeString];
Should be:
patchTypeString = [newPathTypeString retain];
NSLog(@"%@", patchTypeString);
}
}
Best Regards,
Nir Soffer
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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