Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 31, 2013, 09:02 PM   #1
CEBison
macrumors newbie
 
Join Date: Jan 2013
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?
CEBison is offline   0 Reply With Quote
Old Jan 31, 2013, 09:31 PM   #2
robvas
macrumors 68000
 
Join Date: Mar 2009
Location: USA
Post the whole program
robvas is offline   0 Reply With Quote
Old Jan 31, 2013, 10:12 PM   #3
CEBison
Thread Starter
macrumors newbie
 
Join Date: Jan 2013
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
CEBison is offline   0 Reply With Quote
Old Jan 31, 2013, 10:26 PM   #4
mfram
macrumors 6502a
 
Join Date: Jan 2010
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 by mfram; Jan 31, 2013 at 10:32 PM.
mfram is offline   0 Reply With Quote
Old Jan 31, 2013, 10:31 PM   #5
CEBison
Thread Starter
macrumors newbie
 
Join Date: Jan 2013
Thank you for helping me. This Objective-C is a whole different Animal!
CEBison is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 07:59 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC