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?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Luckystrik3
  • 83
  • 1
  • 1
  • 9
  • 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. – rmaddy Feb 23 '13 at 16:47
  • I'm sorry, somehow search didn't lead me this way – Luckystrik3 Feb 23 '13 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. – rmaddy Feb 23 '13 at 17:10

3 Answers3

14

Make sure you allocated memory for kpiStorage.

self.kpiStorage = [[NSMutableArray alloc] init];
Guru
  • 21,652
  • 10
  • 63
  • 102
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]);
Infinity
  • 3,695
  • 2
  • 27
  • 35
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

Community
  • 1
  • 1
abdus.me
  • 1,819
  • 22
  • 34