-2

for some reason my NSMutableArray is always empty, can you guys please help me? here is the code :

-(NSMutableArray*) parseIt: (NSString*) toPars {
NSMutableArray *waranties;
NSString *beginTitle = @"<title>";
NSString *eindTitle = @"</title>";
NSString *title = [self getTitle:toPars start:beginTitle and:eindTitle];
if(title != NULL){
    NSLog(@"de title = %@", title);
    [waranties addObject:title];
    NSLog(@"de title = %@", [waranties objectAtIndex:0]);
}

first nslog shows perfectly what has to be showed, second one shows null; thanks in advance

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
bartvd
  • 3
  • 5
  • 2
    and http://stackoverflow.com/questions/12282808/nsmutablearray-is-empty-after-addobject – rob mayoff Mar 12 '13 at 18:50
  • 2
    and http://stackoverflow.com/questions/11632308/empty-nsmutablearray-not-sure-why – rob mayoff Mar 12 '13 at 18:50
  • 2
    and a hundred others. Please search stackoverflow first. – rob mayoff Mar 12 '13 at 18:52
  • 1
    possible duplicate of [Having trouble adding objects to NSMutableArray](http://stackoverflow.com/q/851926) [Cannot add items to an NSMutableArray ivar](http://stackoverflow.com/q/7125326), [\[NSMutableArray addObject:\] not affecting count](http://stackoverflow.com/q/3683761), [\[NSMutableArray addObject:\] not working](http://stackoverflow.com/q/1827058) – jscs Mar 12 '13 at 20:23

4 Answers4

2

You have to initialize your array.

NSMutableArray *waranties = [[NSMutableArray alloc] init];

Objects not being added to NSMutableArray Objective -C

Community
  • 1
  • 1
DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • is that still needed in ios 6? – bartvd Mar 12 '13 at 18:47
  • Of course it is needed. You have to allocate and initialize an object. Otherwise all you have is a pointer to some random value. What you don't need is releasing the objects, when you use [Automatic Reference Counting (ARC)](http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html), but that is a compiler flag and and not an iOS 6 feature. – DrummerB Mar 12 '13 at 18:50
  • since it works now i guess so.. thanks very much, what needs to be initialised : NSarray to? and what about NSString ans NSinterger? – bartvd Mar 12 '13 at 18:50
  • Objects have to be initialized (NSArray, NSString for instance). Primitives are obviously just primitives, you can't call functions on them (NSInteger for instance). Just look them up in the documentation or the header files. – DrummerB Mar 12 '13 at 18:52
  • just to be precise, references in objc are initializated to nil by default, they dont point to random value like in C, that's why nothing happens (sending a message to nil does not do anything) instead of crashing by accessing a random pointer – Ultrakorne Mar 12 '13 at 19:33
  • @Ultrakorne Under ARC, yes. Otherwise, no. – DrummerB Mar 12 '13 at 19:46
1

You never allocate or initialise the array, e.g.

Replace:

NSMutableArray *waranties;

with:

NSMutableArray *waranties = [[NSMutableArray alloc] init];
TheNextman
  • 12,428
  • 2
  • 36
  • 75
0
NSMutableArray *waranties;

should be

NSMutableArray *waranties = [NSMutableArray array];

You aren’t initializing it, so you’re calling -addObject: on nil. That’s allowed, so you don’t get a crash or anything… but your -objectAtIndex: is then also going to nil, and returning, well, nil.

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
0

Allocate memory.

NSMutableArray *waranties = [[NSMutableArray alloc]init];
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144