views:

84

answers:

2

I am developing iPhone Application.

MyApplicationData.h

#import <Foundation/Foundation.h>


@interface MyApplicationData : NSObject {
    NSMutableArray* appData;
}

@property (retain) NSMutableArray* appData;

-(void)loadData;
-(void)addAppDataItemPrice:(NSString*)price itemCategory:(NSString*)category itemDate:(NSDate*)date;
-(void)forDebug;

+(id)instance;

@end

MyApplicationData.m

#import "MyApplicationData.h"


@implementation MyApplicationData

+ (id)instance
{
    static MyApplicationData* _instance = nil;
    if (!_instance) {
        _instance = [[MyApplicationData alloc] init];
    }
    return _instance;
}

-(void)loadData{
    appData = [NSMutableArray array];
    NSLog(@"%@",appData);
}

-(void)forDebug{
}

-(void)addAppDataItemPrice:(NSString*)price itemCategory:(NSString*)category itemDate:(NSDate*)date{
    NSLog(@"%@", appData);
    [appData addObject:@"1"];
    NSLog(@"%@", appData);
}

@end

another class

[[MyApplicationData instance] loadData];

one another class

[[MyApplicationData instance] addAppDataItemPrice:price itemCategory:category itemDate:date];

log

[Session started at 2009-11-03 21:04:41 +0900.]
2009-11-03 21:04:44.742 XXX[24002:207] (
)
2009-11-03 21:04:46.612 XXX[24002:207] (null)

It is not executed. What is the cause?

+2  A: 

I think this line might be the cause:

appData = [NSMutableArray array];

try this instead:

appData = [[NSMutableArray alloc] init]

You'll want to make sure you release it as well when your MyApplicationData instance is destroyed (not critical in this case since it's a singleton, but still good practice)

Eric Petroelje
+1  A: 

It looks like your array is getting autoreleased after the method:

[[MyApplicationData instance] loadData];

I think it should be a member of the class, you can use the property syntax to help.

Set it up in the header file as:

@property (nonatomic, retain) NSMutableArray *appdata;

Then in the implementation:

@synthesize appdata;

Assign it as follows:

-(void)loadData{
    self.appData = [NSMutableArray array];
}

Don't forget to release it in your dealloc method

When you set it in your code you can call it like the following:

[self.appData addObject:@"1"];
Neil Foley
For completeness sake, this is the line he should use: `self.appData = [NSMutableArray array];`
Pascal