New Mac Programmer

Discussion in 'Mac Programming' started by CEBison, Jan 31, 2013.

  1. macrumors newbie

    Joined:
    Jan 31, 2013
    #1
    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?
     
  2. macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
  3. CEBison, Jan 31, 2013
    Last edited by a moderator: Feb 1, 2013

    thread starter macrumors newbie

    Joined:
    Jan 31, 2013
    #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);
       [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
    };
     
  4. mfram, Jan 31, 2013
    Last edited: Jan 31, 2013

    macrumors 6502a

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #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"];
    
    Then to pull it out:

    Code:
    NSInteger value = [[dict objectForKey:@"Strength"] integerValue];
    
     
  5. thread starter macrumors newbie

    Joined:
    Jan 31, 2013
    #5
    Thank you for helping me. This Objective-C is a whole different Animal!
     

Share This Page