Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

CEBison

macrumors newbie
Original poster
Jan 31, 2013
5
0
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:mad:"Constitution"];

but the program is crashing when trying to add the fourth item. Anyone have an idea?
 

CEBison

macrumors newbie
Original poster
Jan 31, 2013
5
0
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);
   [COLOR="Green"] [CharacterStats setObject:randomNumber forKey:@"Intelligence"]; <-- Where the code is crashing[/COLOR]
    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[COLOR="#808080"]

----------

[/COLOR]Some necessary definitions:
enum Religion
{
    Christian,
    Catholic,
    Atheist,
    Naturalist
};

enum FightingStyle
{
    OneHanded,
    TwoHanded,
    TwoHandedTwoShields,
    Weaponless
};
 
Last edited by a moderator:

mfram

Contributor
Jan 23, 2010
1,307
343
San Diego, CA USA
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"];

Then to pull it out:

Code:
NSInteger value = [[dict objectForKey:@"Strength"] integerValue];
 
Last edited:

CEBison

macrumors newbie
Original poster
Jan 31, 2013
5
0
Thank you for helping me. This Objective-C is a whole different Animal!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.