开发者

iPhone SDk: Cannot add items to a NSMutableDictionary used as a property of a class?

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 ma开发者_运维问答rk -
#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?


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];
....
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜