NSMutableArray

Discussion in 'iOS Programming' started by Nnavick, Dec 12, 2010.

  1. Nnavick macrumors regular

    Joined:
    Oct 13, 2010
    #1
    Hi All,
    I tried to add NSInteger into my NSMutableArray and it's writing a warning


    the code is

    .h File
    Code:
    NSMutableArray *CellsNumber;
    NSInteger *Number;

    .m File
    Code:
     
           CellsNumber = [[NSMutableArray alloc] init]; 
           NSInteger *myInt;
    	myInt=(arc4random()% 50);
    	for(int i = 0 ; i <[CellsNumber count] ; i++) {
    		Number=[CellsNumber objectAtIndex:i][COLOR="blue"]warning: assignment from incompatible pointer type[/COLOR]
    
    
    
    		if(myInt==Number)  [COLOR="blue"]warning: comparison between pointer and integer[/COLOR]
    
    
    
    		myInt=(arc4random()% 50);	
    	}
            [CellsNumber addObject:myInt]; [COLOR="blue"]warning: passing argument 1 of 'addObject:' makes pointer from integer without a cast[/COLOR][/QUOTE]
    what is the problem?
    Thanks
     
  2. ulbador, Dec 12, 2010
    Last edited: Dec 12, 2010

    ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #2
    NSInteger isn't a real object. It is simply a synonym for a long. This is how it is defined on a 64 bit system:

    typedef long NSInteger;


    an NSMutableArray expects objects. An "int" is not an object, it's a primitive. To put a number in an array, use NSNumber and the numberWithInt method.

    Also, when you pull it out for comparisons, you have to convert it back to a primative:

    int myNumber = [[myArray objectAtIndex:i] intValue]; // assuming you've loaded a NSNumber
     

Share This Page