简体   繁体   English

确定NSUInteger是否具有值或为零

[英]Determine if NSUInteger has a value or is nil

I'm trying to use a HKAnchoredObjectQuery on iOS. 我正在尝试在iOS上使用HKAnchoredObjectQuery

To do so I have to provide an anchor to the query which in this case is a NSUInteger . 为此,我必须为查询提供锚点,在本例中为NSUInteger I want to save this integer for queries in the future in the NSUserDefaults and just simply want to use 我想在NSUserDefaults保存此整数以供将来查询,只是想使用

if(anchor != nil) { // do stuff }

But the complier tells me that I'm not allowed to do this because I'm comparing a integer to a pointer and I understand why I'm not able to do this but I don't know how to do it. 但是编译器告诉我,我不允许这样做,因为我正在将整数与指针进行比较,并且我理解为什么我不能执行此操作,但是我不知道如何执行操作。

The error code is this 错误代码是这个

Comparison between pointer and integer ('NSUInteger' (aka 'unsigned long') and 'void *')

Does anyone know how I'm able to determine if my anchor has a value or not? 有谁知道我如何确定我的锚点是否有价值?

NSUInteger is a primitive type, not an object type. NSUInteger是原始类型,而不是对象类型。 NSNumber is an object type. NSNumber是对象类型。 For an NSNumber object, you can check whether it is nil or not. 对于NSNumber对象,可以检查它是否为nil。 An NSUInteger is never nil because it is an integer, not a pointer. NSUInteger永远不会为零,因为它是一个整数,而不是指针。

Many functions that return NSUInteger as a result for a search return the constant NSNotFound if the search failed. 如果搜索失败,许多返回NSUInteger作为搜索结果的函数将返回常量NSNotFound。 NSNotFound is defined as a very large integer (2^31 - 1 for 32 bit, 2^63 - 1) for 64 bit). NSNotFound定义为非常大的整数(32位为2 ^ 31-1,64位为2 ^ 63-1)。

NSUInteger is not an object and cannot be compared to nil. NSUInteger不是对象,不能与nil比较。 In over-simplistic terms, an object is a pointer to a memory location that may contain an value or be nil . 用过于简单的术语来说,对象是指向可能包含值或为nil的内存位置的指针。 NSUInteger is just a value. NSUInteger只是一个值。 It may be 0 , or some other value that you don't expect, but it will have a value. 它可能是0 ,或者是您不期望的其他值,但是会有一个值。

An NSUInteger cannot be nil because it is not an object. NSUInteger不能为nil,因为它不是对象。 The default value on creation would be zero. 创建时的默认值为零。 To save in NSUserDefaults , you could store it as an NSNumber , which could be checked for nil later. 要保存在NSUserDefaults ,可以将其存储为NSNumber ,以后可以检查nil。

To store it in NSUserDefaults : 要将其存储在NSUserDefaults

NSNumber *aNumber = @(anchor);
[[NSUserDefaults standardUserDefaults] setObject:aNumber forKey:aKey];

To get the anchor back from NSUserDefaults : 要从NSUserDefaults获得锚点:

NSNumber *aNumber = [[NSUserDefaults standardUserDefaults] objectForKey:aKey];
if (aNumber != nil) {
    NSUInteger anchor = [aNumber integerValue];
}

NSUInteger or simply int are basically primitive data types and they should be compared with the integer values with in the range of the corresponding data types. NSUInteger或简单地int基本上是原始数据类型,应将它们与相应数据类型范围内的整数值进行比较。 nil is used with referenced objects. nil与引用的对象一起使用。

In your case, as anchor is NSUInteger so you should use integer value for comparison in place of nil as- 在您的情况下,由于anchor是NSUInteger因此您应该使用整数值代替nil as-

if(anchor != 0) {
// do stuff
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM