我试图使用NSMutableArray创建一个简单的命令行tic-tac-toe游戏。
创建了一个名为" Board"用方法" getPosition" (我假设这是获取用户输入的最佳方式) 我要求的位置,然后从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参数,但我收到错误信息:
&#34;指向整数转换的不兼容指针发送&#39; Board * _strong&#34; 参数类型&#39; NSUInteger&#39; (又名&#39;未分配长&#39;)
答案 0 :(得分: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