Problems Adding Object to NSMutableArray

Discussion in 'iPhone/iPad Programming' started by Stunner, Nov 11, 2009.

  1. macrumors newbie

    Joined:
    Sep 14, 2009
    Messages:
    22
    #1
    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!
     
  2. macrumors regular

    Joined:
    Nov 24, 2006
    Messages:
    139
    Location:
    The Netherlands
    #2
    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
     
  3. macrumors member

    Joined:
    Jul 2, 2009
    Messages:
    92
    #3
    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.
     
  4. macrumors regular

    Joined:
    Mar 30, 2008
    Messages:
    127
    Location:
    Uppsala, Sweden
    #4
    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).
     
  5. macrumors newbie

    Joined:
    Sep 14, 2009
    Messages:
    22
    #5
    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.
     
  6. macrumors newbie

    Joined:
    Sep 14, 2009
    Messages:
    22
    #6
    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. :)
     
  7. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Messages:
    15,045
    Location:
    The Centennial State
    #7
    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.
     
  8. macrumors regular

    xsmasher

    Joined:
    Jul 18, 2008
    Messages:
    140
    #8
    Also, I think you want
    NSUInteger counter;
    not
    NSUInteger *counter;

    You don't need a pointer for NSUInteger.
     
  9. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Messages:
    15,045
    Location:
    The Centennial State
    #9
    Yup. As already noted in the second and third posts.
     
  10. macrumors newbie

    Joined:
    Sep 14, 2009
    Messages:
    22
    #10
    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.
     

Share This Page