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 パラメータが必要ですが、次のエラー メッセージが表示されます。

タイプ「NSUInteger」(別名「unassigned long」)のパラメーターに「「Board * _strong」を送信する整数変換への互換性のないポインター

4

1 に答える 1

1

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
于 2014-11-19T19:07:36.833 に答える