将NSNumber插入NSDictionary会导致0

时间:2014-03-29 23:37:24

标签: objective-c

我尝试了很多不同的事情,但我无法理解为什么我放入字典的任何整数都是0。

self.lastDinoIndex = [[NSMutableDictionary alloc] init];
unsigned index = arc4random_uniform(28);
[self.lastDinoIndex
    setObject:[NSNumber numberWithUnsignedInt:index]
    forKey:dinosaur];
NSLog(@"hmmm %d, %d, %d",
    index,
    [[NSNumber numberWithUnsignedInt:index] unsignedIntValue],
    [self.lastDinoIndex[dinosaur] unsignedIntValue]);

打印出来" hmmmm 8,8,0"其中8是随机数,0是我沮丧的来源。

恐龙是一个SKSpriteNode实例。

编辑:整个功能:

-(id)initWithSize:(CGSize)size {

    if (self = [super initWithSize:size]) {
        self.dinoFNs = @[@"broncosoro.png", @"STEGOZAURUS", @"terrorredacter.png", @"treks.png", @"velorappers.png"];
        self.lastDinoIndex = [[NSMutableDictionary alloc] init];

        /* Setup your scene here */
        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

        //That was entirely too difficult to get the background image set properly... holy shit.
        SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"prehistoric_urban_centre"];
        background.anchorPoint = CGPointZero;
        background.position = CGPointZero;
        background.size = self.frame.size;
        [self addChild:background];

        for (NSUInteger i = 0; i < ARRAY_SIZE(points); ++i) {
            SKSpriteNode *anode = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(30, 30)];
            anode.position = points[i];
            [self addChild:anode];
        }

        SKAction *wait = [SKAction waitForDuration:4];
        SKAction *spawnDino = [SKAction runBlock:^{
            SKSpriteNode* dinosaur = [SKSpriteNode spriteNodeWithImageNamed: self.dinoFNs[arc4random_uniform(5)]];
            unsigned index = arc4random_uniform(28);
            [self.lastDinoIndex setObject:[NSNumber numberWithUnsignedInt:index] forKey:dinosaur];
            NSLog(@"hmmm %d, %d, %d",index, [[NSNumber numberWithUnsignedInt:index] unsignedIntValue], [self.lastDinoIndex[dinosaur] unsignedIntValue]);
            dinosaur.position = points[index];
            [self addChild:dinosaur];

            CGFloat duration = 2.0;
            SKAction *moving = [SKAction runBlock:^{
                //get next index
                int curIndex = [self.lastDinoIndex[dinosaur] unsignedIntValue];
                int nextIndex;
                if((curIndex +1) % 5 != 0)
                    nextIndex = ((arc4random_uniform(2) == 1)
                    ? curIndex + 1
                    : curIndex + 5);
                self.lastDinoIndex[dinosaur] = [NSNumber numberWithUnsignedInt: nextIndex];

                NSLog(@"%d => %d", curIndex, nextIndex);

                //TODO: also add option to go down.
                if (nextIndex < 29) {  //at or beyond spawn point
                    NSLog(@"%d: %f, %f", nextIndex, points[nextIndex].x, points[nextIndex].y);
                    [dinosaur runAction:[SKAction moveTo: points[nextIndex] duration:duration]];
                }
                else
                    [dinosaur removeFromParent];
            }];
            SKAction *delayedMoving = [SKAction sequence:@[[SKAction waitForDuration:duration], moving]];
            [dinosaur runAction:[SKAction repeatActionForever: delayedMoving]];
        }];
        SKAction *sequence = [SKAction sequence:@[wait, spawnDino]];

        [self runAction:[SKAction repeatActionForever:sequence]];

    }
    return self;
}

2 个答案:

答案 0 :(得分:3)

self.lastDinoIndex是零。您需要确保在使用之前创建创建的字典。

关于nil的事情是,它只是默默地接受你发送它的任何消息,并返回类型的“零”值,如果它有一个(所以0表示整数,0.0表示浮点数,NULL / nil表示指针和对象)。所以神秘的零通常意味着某些东西是零。

答案 1 :(得分:2)

在我的测试中,问题似乎与isEqual:是如何由SKSpriteNode实现的(一个错误?)。这是我使用的测试代码,

-(void)doStuff {

    self.dinoFNs = @[@"back1.tiff", @"back2.tiff", @"Baldacci.tiff"];
    self.lastDinoIndex = [[NSMutableDictionary alloc] init];

    SKSpriteNode* dinosaur = [SKSpriteNode spriteNodeWithImageNamed: self.dinoFNs[arc4random_uniform(3)]];
    unsigned index = arc4random_uniform(28);
    [self.lastDinoIndex setObject:[NSNumber numberWithUnsignedInt:index] forKey:dinosaur];
    NSLog(@"hmmm %d, %d",index, [self.lastDinoIndex[dinosaur] unsignedIntValue]);
    NSLog(@"%@",self.lastDinoIndex.allKeys[0]);
    NSLog(@"%@",dinosaur);
    NSLog(@"%d", [dinosaur isEqual:self.lastDinoIndex.allKeys[0]]);
}

这是日志,

2014-03-29 18:01:47.811 SpriteNodeDictionaryProblem[2058:60b] hmmm 21, 0
2014-03-29 18:01:47.812 SpriteNodeDictionaryProblem[2058:60b] <SKSpriteNode> name:'(null)' texture:[<SKTexture> 'back2.tiff' (321 x 482)] position:{0, 0} size:{321, 482} rotation:0.00
2014-03-29 18:01:47.812 SpriteNodeDictionaryProblem[2058:60b] <SKSpriteNode> name:'(null)' texture:[<SKTexture> 'back2.tiff' (321 x 482)] position:{0, 0} size:{321, 482} rotation:0.00
2014-03-29 18:01:47.813 SpriteNodeDictionaryProblem[2058:60b] 0

正如您所看到的,当我记录恐龙和self.lastDinoIndex.allKeys [0]时,日志是相同的,但是当我将它们与isEqual:进行比较时,它们会返回不相等。根据NSDictionary类引用,任何符合NSCopying(SKSpriteNode所做的)的对象都应该可以作为键。