NSDictionary的一个整数

问题描述 投票:19回答:3

我正在与怪物的游戏。每个人都有这都将是整数的统计列表。我可以设置每个统计,因为它是自己的变量,但我宁愿让他们在一个NSDictionary因为它们都涉及。我遇到一个问题,当我试图改变每个统计值的。

我有的:

-(id) init {
    self = [super init];
    if(self) {
        stats = [NSDictionary dictionaryWithObjectsAndKeys:
              @"Attack",  0,
              @"Defense", 0,
              @"Special Attack", 0,
              @"Special Defense", 0,
              @"HP", 0, nil];
    }
    return self;
}

我想做的事

-(void) levelUp {
    self.level++;
    [self.stats objectForKey:@"Attack"] += (level * 5);
    [self.stats objectForKey:@"Defense"] += (level * 5);
    [self.stats objectForKey:@"Special Attack"] += (level * 5);
    [self.stats objectForKey:@"Special Defense"] += (level * 5);
    [self.stats objectForKey:@"HP"] += (level * 5);
}

错误我得到

Arithmetic on pointer to interface 'id', which is not a constant size in non-fragile ABI

因此,它似乎很明显,我认为我得到这个问题的原因是,我碰到objectForKey而不是一个整数,返回的对象。于是,我就做我得到不过这给了我另一个错误,特别是在对象上的intValue方法:

Assigning to 'readonly' return result of an objective-c message not allowed

我出于对如何解决这个问题的想法。任何帮助吗?它会更好,只是放弃的念头,以储存所有在一起,只是使用每个统计一个int属性?

objective-c int nsdictionary
3个回答
57
投票
  1. 您只能存储对象,不是基本类型,可可集合类中,所以存储的数字,你需要使用NSNumber对象。
  2. 你需要的,如果你想以后更改的内容使用NSMutableDictionary
  3. 您对dictionaryWithObjectsAndKeys调用有逆转的键和值。
  4. stats对象不被保留,所以它会被释放下一次轮的运行循环(如果你使用手工引用计数,这是)。

你要:

stats = [[NSMutableDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithInt:0], @"Attack",
    [NSNumber numberWithInt:0], @"Defense",
    [NSNumber numberWithInt:0], @"Special Attack",
    [NSNumber numberWithInt:0], @"Special Defense",
    [NSNumber numberWithInt:0], @"HP",
    nil] retain];

为了改变,你需要创建,因为它们是不可变的新NSNumber对象中的值,所以是这样的:

NSNumber *num = [stats objectForKey:@"Attack"];
NSNumber *newNum = [NSNumber numberWithInt:[num intValue] + (level * 5)];
[stats setObject:newNum forKey:@"Attack"];

所有很单调乏味。如果你问我;必须有一个更简单的方法,例如如何创建一个Objective-C类来存储和处理这些东西?


7
投票

NSDictionarys店NSObject*s。为了一个整数使用它们,遗憾的是需要使用像NSNumber。所以,你的初始化将如下所示:

-(id) init {
    self = [super init];
    if(self) {
        stats = [NSDictionary dictionaryWithObjectsAndKeys:
              @"Attack",  [NSNumber numberWithInt:0],
              @"Defense", [NSNumber numberWithInt:0],
              @"Special Attack", [NSNumber numberWithInt:0],
              @"Special Defense", [NSNumber numberWithInt:0],
              @"HP", [NSNumber numberWithInt:0], nil];
    }
    return self;
}

然后,你将不得不对它们进行检索的数字:

NSNumber *atk = [self.stats objectForKey:@"Attack"];
int iAtk = [atk intValue];
[self.stats setObject:[NSNumber numberWithInt:iAtk] forKey:@"Attack"];

编辑

当然,为了做到这一点,self.stats需要有一个NSMutableDictionary


6
投票

适应@ trojanfoe的答案现代Objective-C的漂亮的语法糖:

stats = [@{@"Attack"          : @0,
           @"Defense"         : @0,
           @"Special Attack"  : @0,
           @"Special Defense" : @0,
           @"HP"              : @0} mutableCopy];

而更新的值:

stats[@"Attack"] = @([stats[@"Attack"] intValue] + (level * 5));
© www.soinside.com 2019 - 2024. All rights reserved.