2013-05-03 46 views
-1

有沒有辦法重新分配一個變量,給它一個不同的內存地址裏面一個for循環的OBJÇ - 給變量新的內存地址,每次調用方法

我使用ARC

的主要部分代碼在這裏;

- (void) deal:(BOOL)isDefault 
{ 
    player = [[Player alloc] init]; 
    appDel = [[BlackjackAppDelegate alloc] init]; 
    appDel.playersArray = [[NSMutableArray alloc] init]; 
    appDel.discardPile = [[NSMutableArray alloc] init]; 

    // Initial Deal 

    int i; // Current Card for player 
    int j; // Current Player 

    if (isDefault) 
    { 
     startingCardCount = 7; 
     numberOfPlayers = 4; 
    } 

    for (int temp = 0; temp < numberOfPlayers; temp++) 
    { 
     [self addPlayersWithNumber:temp]; 
    } 

    // Loops through number of cards supposed to be in hand (if default settings: use 7 cards) 
    for (i = 0; i < startingCardCount; i++) 
    { 
     // Loop through number of players (if default settings: use 4 players) 
     for (j = 0; j < numberOfPlayers; j++) 
     { 
      //Player *tempPlayer = [appDel.playersArray objectAtIndex:j]; 
      Card *card = [Card generateCardWithAppDel:appDel]; 
      Player *tempPlayer = [appDel.playersArray objectAtIndex:j]; 

      [tempPlayer.hand addObject:card]; 
      [self refreshPlayerIndex:j withPlayer:tempPlayer]; 
     } 
    } 
} 
- (void) addPlayersWithNumber:(NSInteger)playerNumber 
{ 
    // THIS VARIABLE I WANT TO "REALLOCATE" EACH TIME THIS METHOD IS CALLED 
    Player *tempPlayer = [[Player alloc] init]; 
    tempPlayer.number = playerNumber; 

    [appDel.playersArray addObject:player]; 
} 

感謝您的幫助,您可以給我,我真的很需要這是固定的,我加入到一個數組和交易方法的不同部分操作(如圖)

編輯:

Player.h:

// 
// Player.h 
// Blackjack 
// 
// Created by Zach Ross-Clyne on 02/03/2013. 
// Copyright (c) 2013 Avicode. All rights reserved. 
// 

#import <Foundation/Foundation.h> 
#import "BlackjackAppDelegate.h" 

@interface Player : NSObject 
{ 
    NSInteger type; 
    NSInteger number; 
    NSMutableArray *hand; 
} 

@property (nonatomic, strong) NSMutableArray *hand; 
@property (nonatomic) NSInteger type; 
@property (nonatomic) NSInteger number; 

@end 

Player.m

// 
// Player.m 
// Blackjack 
// 
// Created by Zach Ross-Clyne on 02/03/2013. 
// Copyright (c) 2013 Avicode. All rights reserved. 
// 

#import "Player.h" 

@implementation Player 

@synthesize number, hand, type; 

- (id) init 
{ 
    hand = [[NSMutableArray alloc] init]; 
    return self; 
} 

@end 
+0

您發佈3'for'循環。什麼代碼是相關的,以及你的意思是不同的內存地址? – woz 2013-05-03 19:18:20

+0

你需要更精確地表達你的意思。順便說一句:內循環中有一個明顯的錯誤,你只需要在下一行創建一個'tempPlayer'來覆蓋它。要麼你創建你的tempPlayer(alloc + init舞蹈),要麼你從一些數組中檢索它,但不能同時使用它們。 – verec 2013-05-03 19:20:49

+0

@woz第一個for循環,那個調用'addPlayersWithNumber'方法,它具有變量...並且在調試控制檯中它說'tempPlayer =(Player *)0x0724ccc0' - 我想改變0x0724ccc0位每次被調用,否則其數組中的「相同」對象 – 2013-05-03 19:21:46

回答

4

我認爲你誤解了一個錯誤。請看下面:

Player *tempPlayer = [[Player alloc] init]; 
tempPlayer.number = playerNumber; 

[appDel.playersArray addObject:player]; 

你在做什麼有:

  1. 創建一個播放器,tempPlayer;
  2. 添加實例變量,playerplayersArray

所以,你創造都是獨一無二的,沒有人是永遠把你的陣列中的每個tempPlayer

+0

如果這是唯一的錯誤,我可以看到他是如何做到的。我一直在看這幾分鐘,甚至沒有注意到。好的趕上! – woz 2013-05-03 19:50:41

+1

OOOOOOOOOHHHHHH親愛的!我怎麼會錯過 – 2013-05-03 19:51:00

+0

謝謝你這麼多 – 2013-05-03 19:52:08

1
tempPlayer.number = playerNumber; 
[appDel.playersArray addObject:player]; 

應該是:

tempPlayer.number = playerNumber; 
[appDel.playersArray addObject:tempPlayer]; 
相關問題