Objective-c: NSMutableDictionary setObject not working
Not sure what I am doing wrong here. When I try to check the dictionary either by a specific key or allkeys I either get an error or null. (I know I'm using a string where I could be using a boolean for the conditional I just like having a check like that say true or false instead of YES and NO. Add that to my OCD list. :D ) activePlayer is set in an awakeFromNib method to 1, it can be switched using a popu开发者_开发技巧pbutton between P1 and P2.
- (IBAction)setPlayer:(id)sender {
haserror = @"false";
errmsg = [NSMutableString stringWithCapacity:0];
[errmsg retain];
[errmsg appendString: @"There was a problem setting your team up\n\n"];
thisTeamName = [txtTeamName stringValue];
thisTeamColor = [pdTeamColor itemTitleAtIndex:[pdTeamColor indexOfSelectedItem]];
//validate form
if ([thisTeamName isEqualToString:@""]) {
haserror = @"true";
[errmsg appendString: @"You must enter a team name\n\n"];
}
if ([thisTeamColor isEqualToString:@"Select A Color"]) {
haserror = @"true";
[errmsg appendString: @"You must select a team color\n\n"];
}
//check for errors
if (haserror == @"true") {
[self showAlert: errmsg];
} else {
//set up treasury
treasury = 1000;
//convert to string for display
[lblTreasury setStringValue: [NSString stringWithFormat:@"$%i", treasury] ];
//add items to dictionary
if (activePlayer == @"1") {
[p1TeamData setObject:thisTeamName forKey:@"teamName"];
[p1TeamData setObject:thisTeamColor forKey:@"teamColor"];
[p1TeamData setObject:[NSString stringWithFormat:@"%i", treasury] forKey:@"cash"];
} else {
[p2TeamData setObject:thisTeamName forKey:@"teamName"];
[p2TeamData setObject:thisTeamColor forKey:@"teamColor"];
[p2TeamData setObject:[NSString stringWithFormat:@"%i", treasury] forKey:@"cash"];
}
NSLog(@"%@", [p1TeamData allKeys]);
}
[errmsg release];
}
[Edit: here's the .h file]
@interface GameController :NSObject {
IBOutlet id btnSaveData;
IBOutlet id lblTreasury;
IBOutlet id pdPickPlayer;
IBOutlet id pdTeamColor;
IBOutlet id txtTeamName;
int activePlayer;
NSString* activePlayerName;
NSString* activePlayerTeamColor;
int treasury;
NSMutableDictionary* p1TeamData;
NSMutableDictionary* p2TeamData;
NSArray* players;
NSArray* teamColors;
NSArray* unittypes;
NSString* thisTeamName;
NSString* thisTeamColor;
NSMutableString* errmsg;
NSString* haserror;
}
-(void) awakeFromNib; - (IBAction) getPlayer : (id)sender; - (IBAction) setPlayer : (id)sender; -(void) showAlert : (NSMutableString* ) m; @end
Make sure you initialize the collections in the -initXXX
method. If not, they will be assigned to nil
.
-(id)initXXX:... {
if ((self = [super initYYY:...])) {
...
p1TeamData = [[NSMutableDictionary alloc] init];
p2TeamData = [[NSMutableDictionary alloc] init];
...
}
return self;
}
If all you want are "true" and "false", just define them yourself. It's not a reason to use string instead of BOOL. In fact, Foundation already defined TRUE and FALSE besides YES and NO.
Also, please use an integer for activePlayer
.
You should always compare NSString with -isEqualToString:
, not ==
.
if ([haserror isEqualToString:@"true"])
...
if ([activePlayer isEqualToString:@"1"])
This should be the reason why p1TeamData
is always nil, because activePlayer == @"1"
is unreliable and there could be player-1 stuff assigned to p2TeamData
.
精彩评论