> 
> On 19 Jun 2014, at 4:53 am, Daniel DeCovnick <danhd...@mac.com> wrote:
> 
>> Yes. You can either use key-value coding: [[self valueForKey:myString]
>> release];
> 
> 
>> [value release];
> 
> 
> 
> These invocations of -release appear to be erroneous. Why do you have them
> there? If you think they should be there as a matter of routine, your
> understanding of memory management is probably faulty.
> 
> --Graham
> 

The reason for this is I have several classes with only properties (no
methods other than getters/setters created with synthesize).

The method propertyKeys (below) is used to simplify and shorten the code in
these classes since I want to encode/decode and (upon dealloc), release all
the properties.

In the example below there is only one property, but in reality there are
many.


The interface looks like:

@interface MyClass : NSObject <NSCoding>
{
  NSNumber* someValue;
}
@property (nonatomic, retain) NSNumber* someValue;


The implementation looks like:

@implementation MyClass

@synthesize someValue;

-(id)init
{
    if (self = [super init])
    {
       [self setSomeValue:[NSNumber numberWithInt:3];
    }
    return self;
}


-(void)dealloc
{
    for (NSString* key in [self propertyKeys])
        [[self valueForKey:key] release];
    
    [super dealloc];
}


-(id)initWithCoder:(NSCoder *)coder
{
    if (self = [self init])
    {
        for (NSString* key in [self propertyKeys])
        {
            if ([coder containsValueForKey:key])
                [self setValue:[coder decodeObjectForKey:key] forKey:key];
        }
    }
    return (self);
}


-(void)encodeWithCoder:(NSCoder *)coder;
{
    for (NSString* key in [self propertyKeys])
        [coder encodeObject:[self valueForKey:key] forKey:key];
}


-(NSArray *)propertyKeys
{
    NSMutableArray* propertyKeyArray =
            [[[NSMutableArray alloc] init] autorelease];
    
    unsigned int outCount, i;
    
    objc_property_t* properties =
            class_copyPropertyList([self class], &outCount);

    if (properties)
    {
        for(i = 0; i < outCount; i++)
        {
            objc_property_t property = properties[i];
            const char* propName = property_getName(property);
            if(propName)
            {
                NSString* propertyName =
                    [NSString stringWithUTF8String:propName];
                [propertyKeyArray addObject:propertyName];
            }
        }
        free(properties);
    }
    return (propertyKeyArray);
}



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to