简体   繁体   English

关于NSUinteger和int的问题

[英]Questions about NSUinteger and int

I use JSONModel to capture data from json: 我使用JSONModel从json捕获数据:

@interface BBTCampusBus : JSONModel

@property (strong, nonatomic) NSString * Name;
@property (assign, nonatomic) NSUInteger Latitude;
@property (assign, nonatomic) NSUInteger Longitude;
@property (nonatomic)         BOOL       Direction;
@property (assign, nonatomic) NSUInteger Time;
@property (nonatomic)         BOOL       Stop;
@property (strong, nonatomic) NSString * Station;
@property (assign, nonatomic) NSInteger  StationIndex;
@property (assign, nonatomic) NSUInteger Percent;
@property (nonatomic)         BOOL       Fly;

@end

And I have the following code: 我有以下代码:

for (int i = 0;i < [self.campusBusArray count];i++)
{
    NSLog(@"index at nsuinteger - %@", (NSUInteger)self.campusBusArray[i][@"StationIndex"]);
    NSLog(@"index - %lu", index);
    if ([(NSUInteger)self.campusBusArray[i][[@"StationIndex"] ]== index)
    {
        numberOfBusesCurrentlyAtThisStation++;
    }
}

Actually StationIndex is a 1 or 2 digit integer. 实际上, StationIndex是1或2位数的整数。 For example I have self.campusBusArray[i][@"StationIndex"] == 4, and I have index == 4, then the two NSLog all output 4, but it will not jump into the if block, or the numberOfBusesCurrentlyAtThisStation++ won't be executed. 例如,我有self.campusBusArray[i][@"StationIndex"] == 4,而我的index == 4,则这两个NSLog都输出4,但不会跳入if块,否则numberOfBusesCurrentlyAtThisStation++不会被执行。 Can somebody tell my why? 有人可以告诉我为什么吗?

Lets look at the line: 让我们看一下这行:

NSLog(@"index at nsuinteger - %@", (NSUInteger)self.campusBusArray[i][@"StationIndex"]);

%@ says that an object will be included in the log, one that implements description . %@表示将在日志中包含一个对象,该对象实现description That's good, because the end of the expression dereferences a dictionary, which may only contain objects. 很好,因为表达式的末尾取消了对字典的引用,该字典只能包含对象。

NSUInteger , like int is a scalar type. NSUInteger ,就像int标量类型一样。 Like old-fashioned C, its just a group of bytes in memory whose value is the numerical value of those bytes. 像老式C一样,它只是内存中的一组字节,其值就是这些字节的数值。 An object, even one that represents a number, like NSNumber cannot be cast using the c-style cast (moreover, the precedence of the cast is low, this expression really just casts self , also nonsensical). 不能使用c样式强制转换(即使是表示数字的对象)(例如NSNumber (而且强制转换的优先级较低,该表达式实际上只是强制转换self ,也是荒谬的)。

So it appears that self.campusBusArray is an array of dictionaries (probably the result of parsing JSON describing an array of objects). 因此看来self.campusBusArray是一个字典数组(可能是解析描述对象数组的JSON的结果)。 And it appears you expect those dictionaries to have a key called [@"StationIndex"] with a numerical value. 看来您希望这些词典具有一个名为[@"StationIndex"]的键,并带有数字值。 That must be an NSNumber by the rules of objective-c collections (they hold objects). 根据Objective-C集合的规则(必须持有对象),该值必须NSNumber Therefore: 因此:

NSDictionary *aCampusBusObject = self.campusBusArray[i];     // notice no cast
NSNumber *stationIndex = aCampusBusObject[@"StationIndex"];  // this is an object
NSUInteger stationIndexAsInteger = [stationIndex intValue];  // this is a simple, scalar integer

if (stationIndexAsInteger == 4) {  // this makes sense
}

if (stationIndex == 4) {  // this makes no sense
}

That last line tests to see of the pointer to an object (an address in memory) is equal to 4. Doing scalar math on, or casts on, or comparisons on object pointers almost never makes sense. 最后一行测试是否可以看到指向对象指针 (内存中的地址)等于4。对对象指针进行标量数学运算或对其进行强制转换或进行比较几乎没有任何意义。

Rewriting... 正在改写...

for (int i = 0;i < [self.campusBusArray count];i++)
{
    NSDictionary *aCampusBusObject = self.campusBusArray[i];
    NSNumber *stationIndex = aCampusBusObject[@"StationIndex"];
    NSUInteger stationIndexAsInteger = [stationIndex intValue];

    NSLog(@"index at nsuinteger - %lu", stationIndexAsInteger);
    NSLog(@"index - %lu", index);
    if (stationIndexAsInteger == index)
    {
        numberOfBusesCurrentlyAtThisStation++;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 int或NSInteger到NSUInteger获取索引 - int or NSInteger to NSUInteger for index 另一个&#39;NSUInteger *&#39;(aka&#39;unsigned int *&#39;)与&#39;NSUInteger&#39;(aka&#39;unsigned int&#39;) - Another 'NSUInteger *' (aka 'unsigned int *') vs 'NSUInteger' (aka 'unsigned int') int,NSInteger和NSUInteger之间的区别 - Difference between int, NSInteger and NSUInteger 将NSIntegers(或int)与NSUInteger(或unsigned int)进行比较 - Comparing NSIntegers (or int) to NSUInteger (or unsigned int) 将数组计数分配给int,NSNumber或NSUInteger失败 - Assigning an arrays count to an int, NSNumber or NSUInteger fails 错误的接收器类型“ NSUInteger *”(又名“ unsigned int *”) - Bad receiver type 'NSUInteger *' (aka 'unsigned int *') 不兼容的块指针类型&#39;void(^)()NSUInteger *(*)(int))&#39;到&#39;void(^)(int)&#39; - Incompatible block pointer type 'void (^)()NSUInteger *(*)(int))' to 'void(^)(int)' 比较数组中的整数:(无效的操作数为二进制表达式(“ NSUInteger *”(又名“ unsigned int *”)和“ NSUInteger *”) - Comparing integers in an array: (Invalid operands to binary expression ('NSUInteger *' (aka 'unsigned int *') and 'NSUInteger *') 语义问题:指向整数转换的指针不兼容,将'NSUInteger *'(又名'unsigned int *')发送到'NSUInteger'类型的参数 - Semantic Issue: Incompatible pointer to integer conversion sending 'NSUInteger *' (aka 'unsigned int *') to parameter of type 'NSUInteger' NSUInteger 与 NSInteger、int 与 unsigned 以及类似情况 - NSUInteger vs NSInteger, int vs unsigned, and similar cases
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM