3

I think, I am doing a pretty basic mistake, but I am using an NSMutableArray and this somehow doesn't add the object, I'm sending it its way. I have a property (and synthesize)

@property (nonatomic, strong) NSMutableArray *kpiStorage;

and then:

ExampleObject *obj1 = [[ExampleObject alloc] init];
[kpiStorage addObject:obj1];
ExampleObject *obj2 =  [[ExampleObject alloc] init];
[kpiStorage addObject:obj2];

NSLog(@"kpistorage has:%@", [kpiStorage count]);

and that always returns (null) in the console. What am I misunderstanding?

3
  • 2
    Before posting your question, please be sure to look at the list of related questions that appear. This question has been answered over and over in the past. Commented Feb 23, 2013 at 16:47
  • I'm sorry, somehow search didn't lead me this way Commented Feb 23, 2013 at 17:07
  • I wasn't referring to search. As you type a question, SO shows you lots of possibly related questions. Always check them before submitting the question. Commented Feb 23, 2013 at 17:10

3 Answers 3

14

Make sure you allocated memory for kpiStorage.

self.kpiStorage = [[NSMutableArray alloc] init];
Sign up to request clarification or add additional context in comments.

Comments

4

On top of forgetting to allocated memory for your NSMutableArray, your NSLog formatting is also wrong. Your app will crash when you run it. The following changes are needed

You will need to add

self.kpiStorage = [[NSMutableArray alloc] init];

and change your NSLog to the following

NSLog(@"kpistorage has:%d", [self.kpiStorage count]);

Comments

0

If you are not using ARC make sure you should not create a memory leak in your project. So better way would be allocating like this

NSMutableArray *array = [[NSMutableArray alloc] init];
self.kpiStorage = array;
[array release];

make it a habit to do not directly do

self.kpiStorage = [[NSMutableArray alloc] init];

in this case your property's retain count is incremented by 2. For further reading you can stydy Memory Leak when retaining property

1 Comment

please make it strong type if it is a property !!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.