PDA

View Full Version : Problems Adding Object to NSMutableArray




Stunner
Nov 11, 2009, 06:15 AM
Ey guys, I am having trouble adding an NSDate object to an NSMutableArray and print out its counter. Here is what I have:

NSDate *curTime = [[NSDate alloc] init];
NSMutableArray *arrayOfTimes;
arrayOfTimes = [[NSMutableArray alloc] init];
NSUInteger *counter;
counter = [arrayOfTimes count];
NSLog(@"counter: %@",counter);

This is what I get from the console:

2009-11-11 04:09:34.855 WTC[81711:20b] counter: (null)

I have spent the last hour googling to no success. So I was wondering if anyone here may be able to help me. After I get the counter storing values I would like to be able to iterate through the array and print each value it holds. Here is the code I have for doing that:

while (counter > 0) {
id item = [arrayOfTimes objectAtIndex:counter];
NSLog(@"NSDate: %@",item);
counter--;
}

Does that look good to you guys? No I do not care about what order it prints out in. Thanks in advance!



ChOas
Nov 11, 2009, 06:52 AM
This works fine for me:


NSDate *curTime = [NSDate dateWithString:@"2009-11-11 13:41:00 -0100"];
NSDate *curTime2 = [NSDate dateWithString:@"2009-11-12 13:41:00 -0100"];

NSLog(@"curTime : %@",curTime);
NSLog(@"curTime2: %@",curTime2);

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

[arrayOfTimes addObject: curTime];
[arrayOfTimes addObject: curTime2];

NSUInteger counter;
counter = [arrayOfTimes count];
NSLog(@"counter: %d",counter);

for (NSDate *thisDate in arrayOfTimes) {
NSLog(@"item : %@",thisDate);
};

//or

int i=0;
for (i=0;i<counter;++i) {
NSLog(@"item (method 2): %@",[arrayOfTimes objectAtIndex:i]);
};


Prints:


2009-11-11 13:49:36.679 mytest[116:93] curTime : 2009-11-11 15:41:00 +0100
2009-11-11 13:49:36.680 mytest[116:93] curTime2: 2009-11-12 15:41:00 +0100
2009-11-11 13:49:36.681 mytest[116:93] counter: 2
2009-11-11 13:49:36.682 mytest[116:93] item : 2009-11-11 15:41:00 +0100
2009-11-11 13:49:36.682 mytest[116:93] item : 2009-11-12 15:41:00 +0100
2009-11-11 13:49:36.683 mytest[116:93] item (method 2): 2009-11-11 15:41:00 +0100
2009-11-11 13:49:36.684 mytest[116:93] item (method 2): 2009-11-12 15:41:00 +0100


notes:
Counter is an NSUInteger, not a pointer to an NSUInteger.
In your code you never actually added the date to the array

Troglodyte
Nov 11, 2009, 06:59 AM
NSUInteger *counter;
counter = [arrayOfTimes count];
NSLog(@"counter: %@",counter);

counter isn't a pointer to a string is it?

while (counter > 0) {
id item = [arrayOfTimes objectAtIndex:counter];
NSLog(@"NSDate: %@",item);
counter--;
}

A for each loop would be less verbose.

bredell
Nov 11, 2009, 07:01 AM
Your counter variable is an integer so when logging you should use "%d", not "%@". Also, the index of the array starts with zero so you should iterate from 0 to (counter - 1).

Stunner
Nov 11, 2009, 07:09 AM
Oh wow... I am an idiot... haha. Thanks for pointing out my foolish mistakes man, I really appreciate it. Oh and thanks for that example on enumeration, I totally forgot you could do that with Obj-C.

Stunner
Nov 11, 2009, 07:10 AM
Your counter variable is an integer so when logging you should use "%d", not "%@". Also, the index of the array starts with zero so you should iterate from 0 to (counter - 1).

Yeah I realized that the %d is needed and thanks for pointing out my counter mistake. Maybe I shouldn't be staying up so late... haha. :)

dejo
Nov 11, 2009, 10:03 AM
Maybe I shouldn't be staying up so late... haha. :)
Staying up late is fine. It's the late-night posting that seems to be an issue around here. :D

If you're too tired to understand a problem, you're probably too tired to explain it well enough for the forum to understand. Gnome sane?

You = late-night developer postings in general.

xsmasher
Nov 11, 2009, 10:44 AM
Also, I think you want
NSUInteger counter;
not
NSUInteger *counter;

You don't need a pointer for NSUInteger.

dejo
Nov 11, 2009, 10:52 AM
Also, I think you want
NSUInteger counter;
not
NSUInteger *counter;

You don't need a pointer for NSUInteger.
Yup. As already noted in the second and third posts.

Stunner
Nov 11, 2009, 08:47 PM
Yeah thanks for all the help guys, issue is resolved. Staying up late just caused me to overlook some really easy-to-fix issues that I didn't catch. I think I explained my issue well enough :). But yeah I agree in general, not wise to post or program when not working in your best state of mind.