Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 11, 2009, 06:15 AM   #1
Stunner
macrumors newbie
 
Join Date: Sep 2009
Problems Adding Object to NSMutableArray

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!
Stunner is offline   0 Reply With Quote
Old Nov 11, 2009, 06:52 AM   #2
ChOas
macrumors regular
 
Join Date: Nov 2006
Location: The Netherlands
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
ChOas is offline   0 Reply With Quote
Old Nov 11, 2009, 06:59 AM   #3
Troglodyte
macrumors member
 
Join Date: Jul 2009
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.
Troglodyte is offline   0 Reply With Quote
Old Nov 11, 2009, 07:01 AM   #4
bredell
macrumors regular
 
Join Date: Mar 2008
Location: Uppsala, Sweden
Send a message via Skype™ to bredell
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).
bredell is offline   0 Reply With Quote
Old Nov 11, 2009, 07:09 AM   #5
Stunner
Thread Starter
macrumors newbie
 
Join Date: Sep 2009
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 is offline   0 Reply With Quote
Old Nov 11, 2009, 07:10 AM   #6
Stunner
Thread Starter
macrumors newbie
 
Join Date: Sep 2009
Quote:
Originally Posted by bredell View Post
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.
Stunner is offline   0 Reply With Quote
Old Nov 11, 2009, 10:03 AM   #7
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by Stunner View Post
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.

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.
__________________
dejo is offline   0 Reply With Quote
Old Nov 11, 2009, 10:44 AM   #8
xsmasher
macrumors regular
 
Join Date: Jul 2008
Also, I think you want
NSUInteger counter;
not
NSUInteger *counter;

You don't need a pointer for NSUInteger.
xsmasher is offline   0 Reply With Quote
Old Nov 11, 2009, 10:52 AM   #9
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by xsmasher View Post
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.
__________________
dejo is offline   0 Reply With Quote
Old Nov 11, 2009, 08:47 PM   #10
Stunner
Thread Starter
macrumors newbie
 
Join Date: Sep 2009
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.
Stunner is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
added objects to nsmutablearray, yet the array is null johnmerlino Mac Programming 4 Nov 11, 2011 10:50 PM
Trying to convert object to NSMutableArray johnmerlino iPhone/iPad Programming 4 Nov 11, 2011 03:08 AM
count returns 0 after adding an object to NSMutableArray? ArtOfWarfare iPhone/iPad Programming 6 Sep 7, 2011 02:48 AM
Adding object to dictionary and releasing memory. Marimuthu Mac Programming 5 Jan 31, 2011 09:25 AM
Adding temporary custom objects to NSMutableArray Problems isthisonetaken Mac Programming 16 Nov 24, 2010 08:34 PM


All times are GMT -5. The time now is 10:38 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC