macos NSInteger 设置为 0 但返回 nil

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7707008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:30:35  来源:igfitidea点击:

NSInteger set to 0 but returns nil

objective-cmacoscocoa

提问by BytesGuy

I have an NSInteger in my class like so

我的班级中有一个 NSInteger 像这样

In @interface:

在@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 currentRowis 0so it has set fine, but I can't get the value back.

这样做[self setCurrentRow:0]似乎工作正常,但[self currentRow]由于某种原因使用只返回 null,不知道为什么。当使用断点我可以看到,价值currentRow0所以它有一系列很好,但我不能值回。

回答by paulmelnikow

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

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

An objectis always stored as a pointer, which is the object's location in memory. A pointer is just a number. With NSLog, you can use %pto 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 0xprefix. nilis 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.

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

Primitive typeslike NSIntegeraren't objects. Instead of storing a pointer, usually you just store the value. When you assign an NSIntegervariable, 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-objectwith primitives — they expect objects.

原始类型NSInteger不是对象。通常您只存储值而不是存储指针。分配NSInteger变量时,您会复制该值。您可以使用print. 或者像这样:NSLog("%ld", (long)currentRow)。当你分配一个基元时,你复制了它的值。不要使用%@print-object与原语一起使用——它们需要对象。

(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'tset it.)

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

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 currentRowsomewhere 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]].
  • 如果您正在使用print-object currentRow,请将其更改为print currentRow
  • 如果您正在使用NSLog("%@", currentRow),请将其更改为NSLog(%"ld", (long)currentRow)
  • 如果您在currentRow其他需要对象的地方使用,请将您的实例变量和属性类型更改NSNumber *为对象类型。用 设置它[self setCurrentRow:[NSNumber numberWithInt:0]]

回答by secondcitysaint

NSInteger is not object, it's typedef'd to int or something like that. Thus, if you set currentRow to 0 and then try to get it back, null (aka 0) is totally correct value.

NSInteger 不是对象,它是 typedef 到 int 或类似的东西。因此,如果您将 currentRow 设置为 0,然后尝试将其取回,则 null(又名 0)是完全正确的值。

回答by Mattias Wadman

The getter method will be synthesized as - (NSInteger)currentRowso it should work just fine. But how do you check if it works? With NSLog(@"%@", ...)? Than you should use %d.

getter 方法将被合成,- (NSInteger)currentRow因此它应该可以正常工作。但是你如何检查它是否有效?与NSLog(@"%@", ...)?比你应该使用%d.

If you want it to be an object you should use NSNumberand a retainproperty.

如果您希望它是一个对象,您应该使用它NSNumber和一个retain属性。