Yahllilevy
Yahllilevy

Reputation: 39

Objective C error with replaceObjectAtIndex - Thread 1: EXC_BAD_ACCESS (code=1, address=)

The app crashes when I get to replaceObjectAtIndex, can someone help me?

if ([NSStringName isEqualToString:@"0,0"]) {
    [NSMutableArrayName replaceObjectAtIndex:0 withObject:@"X"];//Thread 1: EXC_BAD_ACCESS (code=1, address=0x522070435258)

Tic-Tac-Toe game - entire project:
Main.m:

#import <Foundation/Foundation.h>
#import "Game.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
    Game *game=[Game new];
    [game play];
}
return 0;
}

Game.h:

#import <Foundation/Foundation.h>
#import "Board.h"
#import "Players.h"
@interface Game : NSObject
{
    NSMutableArray *boardPositions;
}
@property BOOL playerTurn;
@property BOOL won;
-(void)play;
+(void)winningWithBoardPositions:(NSMutableArray*)boardPositions AndPlayerTurn:(BOOL)playerTurn;
@end

Game.m:

#import "Game.h"
@implementation Game
@synthesize playerTurn;
@synthesize won;
-(void)play
{
    //Setting playerTurn to YES ('X' turn) and won to NO
    self.playerTurn=YES;
    self.won=NO;
    //Initializing the initial blank board with spaces and printing it
    boardPositions=[[NSMutableArray alloc]initWithObjects:@" ",@" ",@" ",@" ",@" ",@" ",@" ",@" ",@" ", nil];
[Board printBoardSpaces:boardPositions];

//Playing X/O turn
for (int i=0; i<9||!won; i++) {
    [Players playTurnWithPlayerTurn:playerTurn andBoardPositions:boardPositions];
    [Board printBoardSpaces:boardPositions];
    [Game winningWithBoardPositions:boardPositions AndPlayerTurn:playerTurn];
    self.playerTurn=!self.playerTurn;
}
if (!won) {
    NSLog(@"Game Over");
}
}
+(void)winningWithBoardPositions:(NSMutableArray*)boardPositions AndPlayerTurn:(BOOL)playerTurn
{
//First width row (indexes 0,1,2) winning check and print
if ([[boardPositions objectAtIndex:0]isEqualTo:[boardPositions objectAtIndex:1]]&&[[boardPositions objectAtIndex:0]isEqualTo:[boardPositions objectAtIndex:3]]&&![[boardPositions objectAtIndex:0] isEqual:@" "]) {
    NSLog(@"%c is the winner!",playerTurn?'O':'X');
}
//second width row (indexes 3,4,5) winning check and print
if ([[boardPositions objectAtIndex:3]isEqualTo:[boardPositions objectAtIndex:4]]&&[[boardPositions objectAtIndex:3]isEqualTo:[boardPositions objectAtIndex:5]]&&![[boardPositions objectAtIndex:3] isEqual:@" "]) {
    NSLog(@"%c is the winner!",playerTurn?'O':'X');
}
//Third width row (indexes 6,7,8) winning check and print
if ([[boardPositions objectAtIndex:6]isEqualTo:[boardPositions objectAtIndex:7]]&&[[boardPositions objectAtIndex:6]isEqualTo:[boardPositions objectAtIndex:8]]&&![[boardPositions objectAtIndex:6] isEqual:@" "]) {
    NSLog(@"%c is the winner!",playerTurn?'O':'X');
}
//First length row (0,3,6 indexes) winning check and print
if ([[boardPositions objectAtIndex:0]isEqualTo:[boardPositions objectAtIndex:3]]&&[[boardPositions objectAtIndex:0]isEqualTo:[boardPositions objectAtIndex:6]]&&![[boardPositions objectAtIndex:0] isEqual:@" "]) {
    NSLog(@"%c is the winner!",playerTurn?'O':'X');
}
//Second length row (1,4,7 indexes) winning check and print
if ([[boardPositions objectAtIndex:1]isEqualTo:[boardPositions objectAtIndex:4]]&&[[boardPositions objectAtIndex:1]isEqualTo:[boardPositions objectAtIndex:7]]&&![[boardPositions objectAtIndex:1] isEqual:@" "]) {
    NSLog(@"%c is the winner!",playerTurn?'O':'X');
}
//Third length row (2,5,8 indexes) winning check and print
if ([[boardPositions objectAtIndex:2]isEqualTo:[boardPositions objectAtIndex:5]]&&[[boardPositions objectAtIndex:2]isEqualTo:[boardPositions objectAtIndex:8]]&&![[boardPositions objectAtIndex:2] isEqual:@" "]) {
    NSLog(@"%c is the winner!",playerTurn?'O':'X');
}
//Left diagonal row (indexes 0,4,8) winning check and print
if ([[boardPositions objectAtIndex:0] isEqualTo:[boardPositions objectAtIndex:4]]&&[[boardPositions objectAtIndex:0]isEqualTo:[boardPositions objectAtIndex:8]]&&![[boardPositions objectAtIndex:0] isEqual:@" "]) {
    NSLog(@"%c is the winner!",playerTurn?'O':'X');
}
//Right diagonal row (indexes 2,4,6) winning check and print
if ([[boardPositions objectAtIndex:2] isEqualTo:[boardPositions objectAtIndex:4]]&&[[boardPositions objectAtIndex:2]isEqualTo:[boardPositions objectAtIndex:6]]&&![[boardPositions objectAtIndex:2] isEqual:@" "]) {
    NSLog(@"%c is the winner!",playerTurn?'O':'X');
}
}

@end

Players.h:

#import <Foundation/Foundation.h>
@interface Players : NSObject
+(void) playTurnWithPlayerTurn:(BOOL)playerTurn andBoardPositions:(NSMutableArray*)boardPositions;
@end

Players.m:

#import "Players.h"
@implementation Players

+(void) playTurnWithPlayerTurn:(BOOL)playerTurn andBoardPositions:(NSMutableArray*)boardPositions
{

//Printing "X turn" or "O turn"
if (playerTurn) {
    NSLog(@"O turn");
}
else
    NSLog(@"X turn");
NSLog(@"Where do you want to insert the %c?",playerTurn?'O':'X');
char input[1];
gets(input);
NSString *inputString=[NSString stringWithUTF8String:input];

//Checking user's input and implementing his choice to the board
if ([inputString isEqualToString:@"0,0"]) {
    [boardPositions replaceObjectAtIndex:0 withObject:playerTurn?@"O":@"X"];
}
if ([inputString isEqualToString:@"0,1"]) {
    [boardPositions replaceObjectAtIndex:1 withObject:playerTurn?@"O":@"X"];
}
if ([inputString isEqualToString:@"0,2"]) {
    [boardPositions replaceObjectAtIndex:2 withObject:playerTurn?@"O":@"X"];
}
if ([inputString isEqualToString:@"1,0"]) {
    [boardPositions replaceObjectAtIndex:3 withObject:playerTurn?@"O":@"X"];
}
if ([inputString isEqualToString:@"1,1"]) {
    [boardPositions replaceObjectAtIndex:4 withObject:playerTurn?@"O":@"X"];
}
if ([inputString isEqualToString:@"1,2"]) {
    [boardPositions replaceObjectAtIndex:5 withObject:playerTurn?@"O":@"X"];
}
if ([inputString isEqualToString:@"2,0"]) {
    [boardPositions replaceObjectAtIndex:6 withObject:playerTurn?@"O":@"X"];
}
if ([inputString isEqualToString:@"2,1"]) {
    [boardPositions replaceObjectAtIndex:7 withObject:playerTurn?@"O":@"X"];
}
if ([inputString isEqualToString:@"2,2"]) {
    [boardPositions replaceObjectAtIndex:8 withObject:playerTurn?@"O":@"X"];
}

}
@end

Board.h:

#import <Foundation/Foundation.h>
@interface Board : NSObject
+(void)printBoardSpaces:(NSMutableArray*)boardPositions;
@end

Board.m:

#import "Board.h"
@implementation Board
+(void)printBoardSpaces:(NSMutableArray*)boardPositions {
    NSLog(@"\n%@|%@|%@\n%@|%@|%@\n%@|%@|%@",[boardPositions objectAtIndex:0],[boardPositions objectAtIndex:1],[boardPositions objectAtIndex:2],[boardPositions objectAtIndex:3],[boardPositions objectAtIndex:4],[boardPositions objectAtIndex:5],[boardPositions objectAtIndex:6],[boardPositions objectAtIndex:7],[boardPositions objectAtIndex:8]);
}
@end

I can't understand what's wrong with this? The error pops up every run, when it's get to 'replaceObjectAtIndex' in a green label under the code line. If someone can read it and tell me what's wrong that will really help me! Thanks alot!

Upvotes: 0

Views: 599

Answers (2)

Kavita
Kavita

Reputation: 176

May be you are not yet initialise array yet.

This code may help you.

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"one",@"two",@"three",@"four", nil];

    NSString *string = @"0,0";
    if ([string isEqualToString:@"0,0"]) 
{

 [array replaceObjectAtIndex:0 withObject:@"X"];

 }

Upvotes: 1

Vishnuvardhan
Vishnuvardhan

Reputation: 5107

Try like this.

if(array!=nil&&array.count>0){
        [array replaceObjectAtIndex:0 withObject:@"X"];
    }

Upvotes: 0

Related Questions