目标 c - 将 NSNumber 插入 NSDictionary 结果为 0



我尝试了很多不同的东西,我不明白为什么我放入字典中的任何整数都显示为 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]);

这打印出"嗯8,8,0",其中8是随机数,0是我沮丧的根源。

Dinosaur是一个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;
}

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

关于 nil 的事情是,它只是静默地接受您发送的任何消息,如果它有一个,则返回类型的"零"值(因此整数为 0,浮点数为 0.0,指针和对象为 NULL/nil)。所以神秘的零通常意味着某件事是零的。

在我的测试中,问题似乎在于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 就是这样做的),都应该可以作为键。

最新更新