|
|
#1 |
|
New Mac Programmer
I have a good background in C and C++ programming, but I am new to Objective C. I'm writing a program and I am trying to add items to an NSMutableDictionary...
Created this way: CharacterStats = [[NSMutableDictionary alloc] init]; I am adding items this way: [CharacterStats setObject:randomNumber forKey:@"Constitution"]; but the program is crashing when trying to add the fourth item. Anyone have an idea? |
|
|
|
0
|
|
|
#2 |
|
Post the whole program
|
|
|
|
0
|
|
|
#3 |
|
Code:
#import "Character.h"
#import "Item.h"
#include <time.h>
@implementation Character
-(id)init
{
if (self = [super init])
{
Name = [[NSMutableString alloc] init];
CharacterStats = [[NSMutableDictionary alloc] init];
srand((int)time(0));
[self setupValues];
}
return self;
}
- (void) setupValues
{
NSUInteger initialWeight = 150;
NSUInteger randomNumber = ((rand()%30) + 10);
[CharacterStats setObject:randomNumber forKey:@"Strength"];
randomNumber = ((rand()%30) + 10);
[CharacterStats setObject:randomNumber forKey:@"Dexterity"];
randomNumber = ((rand()%30) + 10);
[CharacterStats setObject:randomNumber forKey:@"Constitution"];
randomNumber = ((rand()%30) + 10);
[CharacterStats setObject:randomNumber forKey:@"Intelligence"]; <-- Where the code is crashing
randomNumber = ((rand()%30) + 10);
[CharacterStats setObject:randomNumber forKey:@"Charisma"];
//From a scale of 0 to 100, your morality. 0 = Evil. 100 = Good.
[CharacterStats setObject:50 forKey:@"Morality"];
[CharacterStats setObject:Weaponless forKey:@"Fighting Style"];
[CharacterStats setObject:Atheist forKey:@"Religion"];
[CharacterStats setObject:initialWeight forKey:@"Weight"];
Platinum = 0;
Gold = 0;
Silver = 0;
Copper = 0;
}
@end
Character.h:
@interface Character : NSObject
{
NSMutableString *Name;
NSMutableDictionary *CharacterStats;
struct ItemSlots CharacterSlots;
unsigned int Platinum;
unsigned int Gold;
unsigned int Silver;
unsigned int Copper;
Item *NullItem;
}
//Constructors:
-(id) init;
-(void) setupValues;
@end
----------
Some necessary definitions:
enum Religion
{
Christian,
Catholic,
Atheist,
Naturalist
};
enum FightingStyle
{
OneHanded,
TwoHanded,
TwoHandedTwoShields,
Weaponless
};
Last edited by stridemat; Feb 1, 2013 at 02:26 AM. Reason: Please use code tags |
|
|
|
0
|
|
|
#4 |
|
NSDictionary and NSArray expect you to hand them objects. You are trying to give them integer values. If you want to put numerical values into an NSDictionary, you'll need to wrap them in NSNumber objects.
Something like: Code:
[dict setObject:[NSNumber numberWithInteger:randomNumber] forKey:@"Strength"]; Code:
NSInteger value = [[dict objectForKey:@"Strength"] integerValue]; Last edited by mfram; Jan 31, 2013 at 10:32 PM. |
|
|
|
0
|
|
|
#5 |
|
Thank you for helping me. This Objective-C is a whole different Animal!
|
|
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 11:02 PM.






Linear Mode
