另一个'NSUInteger *'(又名'unsigned int *')vs'NSUInteger'(又名'unsigned int')

时间:2012-04-01 01:55:05

标签: objective-c pointers primitive

我有以下实例方法应该返回NSUInteger:

    - (NSUInteger)getCurAnsPos {

    NSUInteger CAP = 999;
    NSArray *curCAS = [[GameData gameData].curData valueForKey:@"clueAnsState"];
    for (NSInteger idx = 0; idx < 8; idx++) {
        NSString *temp = [curCAS objectAtIndex:idx];
        if ([temp isEqualToString:@"2"]) {
            CAP = [curCAS indexOfObject:temp];
            NSLog(@"CAP = ");
            NSLog(@"%i", CAP);
        } // only one curCAS should be = 2
    }
    return CAP;
}

我在问题标题中收到错误。 CAP计算的值是正确的,但返回类型显然是错误的。我不明白为什么。我知道这与指针和原语有关,但在阅读了SO档案后,我认为这应该有效。可能是CAP = [curCAS indexOfObject:temp];返回CAP作为指针(实际上,它必须是),但我仍然没有看到如何更改它。我需要的是我可以在objectAtIndex:CAP语句中使用的东西。感谢。

1 个答案:

答案 0 :(得分:0)

这段代码对我来说没有多大意义:

    NSString *temp = [curCAS objectAtIndex:idx];
    if ([temp isEqualToString:@"2"]) {
        CAP = [curCAS indexOfObject:temp];
        NSLog(@"CAP = ");
        NSLog(@"%i", CAP);
    } // only one curCAS should be = 2

temp是索引idx

的对象

如果temp == "2",则CAP是对象temp的索引(必须为idx,因此无需操作)

该方法确实返回一个数字,而不是指向数字的指针:http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/doc/uid/20000137-BABDHGFB

相关问题