[英]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.