Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Stunner

macrumors newbie
Original poster
Sep 14, 2009
22
0
Ey guys, I am having trouble adding an NSDate object to an NSMutableArray and print out its counter. Here is what I have:
Code:
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:
Code:
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:
Code:
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!
 
This works fine for me:

Code:
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:

Code:
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
 
Code:
NSUInteger *counter;
counter = [arrayOfTimes count];
NSLog(@"counter: %@",counter);
counter isn't a pointer to a string is it?
Code:
while (counter > 0) {
		id item = [arrayOfTimes objectAtIndex:counter];
		NSLog(@"NSDate: %@",item);
		counter--;
	}
A for each loop would be less verbose.
 
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).
 
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.
 
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. :)
 
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.
 
Also, I think you want
NSUInteger counter;
not
NSUInteger *counter;

You don't need a pointer for NSUInteger.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.