本文介绍了NSInteger设置为0,但返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类中有一个NSInteger,如下



在@interface中:

 code> NSInteger currentRow; 

@property(assign)NSInteger currentRow;

在@implementation:

  @synthesize currentRow; 

[self setCurrentRow:0] 工作正常,但使用 [self currentRow] 只是由于某种原因返回null,不知道为什么。当使用断点时,我可以看到 currentRow 的值是 0 所以它已设置好,但我不能。

解决方案

在Objective-C中,重要的是要区分对象和原始类型。

对象始终作为指针存储,它是对象在内存中的位置。指针只是一个数字。使用 NSLog ,您可以使用%p 查看此值。你可以在调试器中显示它,像这样: print myObject 。指针显示为十六进制数字,并带有 0x 前缀。 nil 实质上是位置零( 0x0000 )。当你分配任何类型的对象时,你会得到一个不为零的指针。当您将对象分配给变量时,您只需复制内存地址,而不是复制对象。使用 NSLog ,您可以使用%@ 打印出对象的描述。在调试器中,像这样: print-object myObject



原始类型 NSInteger 不是对象。而不是存储指针,通常你只是存储的值。当您分配一个 NSInteger 变量时,您将创建一个值的副本。您可以使用 print 在调试器中查看该值。或者像这样: NSLog(%ld,(long)currentRow)。当您分配一个基元时,您复制其值。不要对原语使用%@ print-object - 它们期望对象。



(我说通常你只是存储值,因为你也可以指向原始类型,在像你的情况下,这是没有必要的。)



[self currentRow] 返回0,就像你设置它。 (此外,因为Objective-C保证实例变量的初始化,即使你不设置它,它也会返回0)。



问题是,你期望一个指向一个对象的指针。如何修正程式码取决于您的使用方式:




  • 如果您使用 print-object currentRow ,将其更改为打印currentRow

  • 如果使用 NSLog(%@,currentRow),将其更改为 NSLog(%ld,(long)currentRow)

  • 如果你在其他地方使用 currentRow ,那么你的实例变量和属性类型改为 NSNumber * ,一个对象类型。使用 [self setCurrentRow:[NSNumber numberWithInt:0]] 设置。


I have an NSInteger in my class like so

In @interface:

NSInteger currentRow;

@property (assign) NSInteger currentRow;

In @implementation:

@synthesize currentRow;

Doing [self setCurrentRow:0] seems to work fine, but using [self currentRow] just returns null for some reason, not sure why. When using breakpoints I can see that the value for currentRow is 0 so it has set fine, but I can't get the value back.

解决方案

In Objective-C, it's important that you distinguish between objects and primitive types.

An object is always stored as a pointer, which is the object's location in memory. A pointer is just a number. With NSLog, you can use %p to see this value. You can display it in the debugger too, like this: print myObject. A pointer is displayed as a hexadecimal number, with a 0x prefix. nil is essentially location zero (0x0000). When you allocate any kind of object, you'll get a pointer which isn't zero. When you assign an object to a variable, you are simply copying the memory address, not duplicating the object. With NSLog, you can use %@ to print out an object's description. In the debugger, like this: print-object myObject.

Primitive types like NSInteger aren't objects. Instead of storing a pointer, usually you just store the value. When you assign an NSInteger variable, you make a copy of the value. You can see the value in the debugger using print. Or like this: NSLog("%ld", (long)currentRow). When you assign a primitive, you copy its value. Don't use %@ or print-object with primitives — they expect objects.

(I say "usually you just store the value," because you can make pointers to primitive types, too. In situations like yours however it's not necessary.)

[self currentRow] returns 0, just like you set it. (Furthermore, because Objective-C guarantees initialization of instance variables, it'll return 0 even if you don't set it.)

The problem is that you're expecting a pointer to an object. How you fix your code depends on how you're using it:

  • If you're using print-object currentRow, change it to print currentRow.
  • If you're using NSLog("%@", currentRow), change it to NSLog(%"ld", (long)currentRow).
  • If you're using currentRow somewhere else, where an object is required, change your instance variable and property types to NSNumber *, an object type. Set it with [self setCurrentRow:[NSNumber numberWithInt:0]].

这篇关于NSInteger设置为0,但返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 20:15