有沒有辦法重新分配一個變量,給它一個不同的內存地址裏面一個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
您發佈3'for'循環。什麼代碼是相關的,以及你的意思是不同的內存地址? – woz 2013-05-03 19:18:20
你需要更精確地表達你的意思。順便說一句:內循環中有一個明顯的錯誤,你只需要在下一行創建一個'tempPlayer'來覆蓋它。要麼你創建你的tempPlayer(alloc + init舞蹈),要麼你從一些數組中檢索它,但不能同時使用它們。 – verec 2013-05-03 19:20:49
@woz第一個for循環,那個調用'addPlayersWithNumber'方法,它具有變量...並且在調試控制檯中它說'tempPlayer =(Player *)0x0724ccc0' - 我想改變0x0724ccc0位每次被調用,否則其數組中的「相同」對象 – 2013-05-03 19:21:46