views:

54

answers:

1

When using a property of type NSMutableDictionary outside of the class I cannot add objects.

This is the class.h file

@interface cSummary : NSObject 
{
@public
    NSMutableDictionary*    PaymentMethodSalesTotals;
}

@property (nonatomic, retain) NSMutableDictionary*  PaymentMethodSalesTotals;

@end

The class.m file is like this

    #import "cSummary.h"


@implementation cSummary

@synthesize PaymentMethodSalesTotals;

#pragma mark -
#pragma mark Initialisation

-(id)init
{
    return [super init];

    PaymentMethodSalesTotals = [[ NSMutableDictionary alloc] init];

    [PaymentMethodSalesTotals setObject:[NSNumber numberWithFloat:0.0f] forKey:[NSNumber numberWithInt:10]];
    [PaymentMethodSalesTotals setObject:[NSNumber numberWithFloat:0.0f] forKey:[NSNumber numberWithInt:20]];
    [PaymentMethodSalesTotals setObject:[NSNumber numberWithFloat:0.0f] forKey:[NSNumber numberWithInt:30]];
}

#pragma mark -
#pragma mark Memory management

- (void)dealloc 
{
    if (PaymentMethodSalesTotals != nil)
        [PaymentMethodSalesTotals release];

    [super dealloc];
}


@end

Then this is used in another class like this

cSummary* oSummary = [[cSummary alloc] init];

NSNumber* numPrice = [NSNumber numberWithFloat:150.0f];
[oSummary.PaymentMethodSalesTotals setObject:numPrice forKey:[NSNumber numberWithInt:10]];

If I view this in debug the PaymentMethodSalesTotals collection still has no values in it?

+2  A: 

In init you return first so the dictionary isn't allocated/instantiated. Move the return to the end of the method.

-(id)init
{
    return [super init]; //!!!!!!!!!!!!

    PaymentMethodSalesTotals = [[ NSMutableDictionary alloc] init];
....
}
Alin
Ha ha ha, oh my gosh, very embarrassing, yes that is it, sometimes I can't see the wood for the trees. I'll accept your answer asap :$
Eigo