Using NSUInteger Input to insertObject:___ atIndex:___

46 views Asked by At

I'm trying to create a simple commandline tic-tac-toe game using an NSMutableArray.

Created a class called "Board" with the method "getPosition" (I'm assuming this is the best way to get a user input) I'm asking for position, then casting from int to NSUInteger)

#import "Board.h"

@implementation Board

-(void)getPosition;
{
    int enteredPosition;
    scanf("%i", &enteredPosition);
    NSUInteger nsEnteredPosition = (NSUInteger ) enteredPosition;
    NSLog(@"Position = %lu", (unsigned long)nsEnteredPosition);
}



#import <Foundation/Foundation.h>
#import "Board.h"


int main(int argc, const char * argv[])
{

    @autoreleasepool {

        NSString *currentPlayer;
        NSMutableArray *gameBoard=[[NSMutableArray alloc] initWithCapacity:9];

        for(int i; i<=2; i++)
        {
            if(i %2)
            {
                currentPlayer=@"X";
            }
            else
            {
                currentPlayer=@"O";
            }

        NSLog(@"Player %@, select an open spot 1 - 9 on the board", currentPlayer);
        Board *currentPosition = [[Board alloc] init];
        [currentPosition getPosition];
        [gameBoard insertObject:currentPlayer atIndex:currentPosition]; //this is where i have a problem
        }

As I understand it atIndex requires an NSUInteger parameter, but I'm receiving the error message:

"Incompatible pointer to integer conversion sending 'Board *_strong" to parameter of type 'NSUInteger' (aka 'unassigned long')

1

There are 1 answers

3
Casey Fleser On

You're using currentPosition as your index which is a Board object. Perhaps [currentPosition getPosition] is supposed to return an NSUInteger. If so, try rewriting the last portion of your code like this:

Board *theBoard = [[Board alloc] init];
NSUInteger currentPosition = [theBoard getPosition];
[gameBoard insertObject:currentPlayer atIndex:currentPosition]; //this is where i have a problem