简体   繁体   English

使用NSUInteger输入插入对象:___ atIndex:___

[英]Using NSUInteger Input to insertObject:___ atIndex:___

I'm trying to create a simple commandline tic-tac-toe game using an NSMutableArray. 我正在尝试使用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) 使用方法“ 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
        }

As I understand it atIndex requires an NSUInteger parameter, but I'm receiving the error message: 据我了解,atIndex需要NSUInteger参数,但是我收到错误消息:

"Incompatible pointer to integer conversion sending 'Board *_strong" to parameter of type 'NSUInteger' (aka 'unassigned long') “不兼容的指针,将'Board * _strong'发送给'NSUInteger'类型的参数(也就是'unssigned long')

You're using currentPosition as your index which is a Board object. 您使用currentPosition作为Board对象的索引。 Perhaps [currentPosition getPosition] is supposed to return an NSUInteger . 也许[currentPosition getPosition]应该返回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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM