带有整数值的NSDictionary [英] NSDictionary With Integer Values

查看:204
本文介绍了带有整数值的NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和怪物一起玩游戏。每个人都有一个统计数据列表,这些数据都将是整数。我可以将每个stat设置为它自己的变量,但我更喜欢将它们保存在NSDictionary中,因为它们都是相关的。当我试图更改每个统计数据的值时,我遇到了问题。

I'm working on a game with monsters. Each one has a list of stats that are all going to be ints. I can set up each stat as it's own variable but I'd prefer to keep them in an NSDictionary since they are all related. I'm running into a problem when I'm trying to change the value's of each stat.

我有什么:

-(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方法,但这给了我另一个错误,特别是:

So it seems obvious to me that the reason I'm getting the problem is that I'm getting an object returned from objectForKey instead of an integer. So I tried to do the intValue method on the object I'm getting but that gave me another error, specifically:

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

我不知道如何解决这个问题。有帮助吗?放弃将它们全部存储在一起并且只为每个统计数据使用int属性会更好吗?

I'm out of ideas for how to fix this. Any help? Would it be better to just give up the idea to store them all together and just use an int property for each stat?

推荐答案


  1. 你只能在Cocoa集合类中存储对象,而不是基元,所以要存储你需要使用的数字 NSNumber 对象。

  2. 如果您希望稍后更改内容,则需要使用 NSMutableDictionary

  3. 您对<$的致电c $ c> dictionaryWithObjectsAndKeys 键和值相反。

  4. 您的 stats 对象未被保留,所以下次循环运行时会释放它(如果你使用手动引用计数,那就是)。

  1. You can only store objects, not primitives, within Cocoa collection classes, so to store numbers you need to use NSNumber objects.
  2. You need to use an NSMutableDictionary if you wish to change the contents later.
  3. Your call to dictionaryWithObjectsAndKeys has the keys and values reversed.
  4. Your stats object is not being retained, so it will be released next time round the run loop (if you're using manual reference counting, that is).

你想要:

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];

适用于Swift 4.x

intValue as NSNumber

为了更改创建新 NSNumber 对象所需的值,因为它们是不可变的,所以类似于:

In order to change the values you need to create a new NSNumber object as they immutable, so something like:

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

如果你问我,那一切都很乏味;必须有一个更简单的方法,例如如何创建一个Objective-C类来存储和操作这些东西?

All pretty tedious if you ask me; there must be an easier way, for example how about creating an Objective-C class to store and manipulate this stuff?

这篇关于带有整数值的NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆