0

我正在尝试使用 NSMutableArray 创建一个简单的命令行井字游戏。

使用“getPosition”方法创建了一个名为“Board”的类(我假设这是获取用户输入的最佳方式)我要求位置,然后从 int 转换为 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
        }

据我了解, atIndex 需要一个 NSUInteger 参数,但我收到错误消息:

“指向整数转换的不兼容指针将'Board *_strong”发送到'NSUInteger'类型的参数(又名'unassigned long')

4

1 回答 1

1

您正在使用currentPosition作为Board对象的索引。也许[currentPosition getPosition]应该返回一个NSUInteger. 如果是这样,请尝试像这样重写代码的最后一部分:

Board *theBoard = [[Board alloc] init];
NSUInteger currentPosition = [theBoard getPosition];
[gameBoard insertObject:currentPlayer atIndex:currentPosition]; //this is where i have a problem
于 2014-11-19T19:07:36.833 回答