simple type cast in a loop?

Discussion in 'Mac Programming' started by mlecho, Aug 1, 2009.

  1. mlecho macrumors newbie

    Joined:
    Mar 31, 2004
    #1
    Sorry all, i am new to ObjectiveC-> In other programming languages, i like to cast the variable i am working with when in a loop. I have a class (FeedItem) with a property 'description' . I know that my NSMutableArray (menuList) has 15-20 of these FeedItem pushed earlier in my code (i can also see them in the debugger). However, when i prepare to do this loop, i get an error: "error: incompatible types in initialization"

    why can't i do this?
    Code:
    for(int b =0;b<[menuList count];b++)
    	{
    		FeedItem *fi = menuList[b];
    		NSLog(@"----->@%",fi.description);		
    	}
    again, i am new to objective c, so is it my loop syntax?
     
  2. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #2
    Assuming that menuList of of type NSArray*, you cannot use the standard bracketed index method to access its elements; you must use the objectAtIndex: method.
    For example, say I have an array
    Code:
    NSArray *foo
    with three elements, @"zero", @"one" and @"two" (at their respective indices). Executing the snippet
    Code:
    NSLog( @"%@", [foo objectAtIndex:0] );
    will print "zero" to the console.
     
  3. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    If you don't need backwards compatibility, the preferred way to loop through an array is:
    Code:
    for (FeedItem *fi in menuList)
    {
        NSLog(@"----->@%",fi.description);		
    }
     
  4. mlecho thread starter macrumors newbie

    Joined:
    Mar 31, 2004
    #4
    thanks guys...both are perfect, and now this new-b can get back on track
     
  5. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #5
    Just like C#, nice :).
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    And Java, and Perl, and...
    http://en.wikipedia.org/wiki/Foreach

    It was something that was definitely missing from Objective-C, and a very welcome addition to 2.0.

    -Lee
     

Share This Page