xcode 在 NSInteger 返回类型方法上返回 nil

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2023542/
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-09-14 19:03:15  来源:igfitidea点击:

Return nil on NSInteger return type method

iphoneobjective-cxcodeuitableviewnull

提问by ff10

I have the following UITableView DataSource method:

我有以下 UITableView DataSource 方法:

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 

On some items (i.e. section index titles) I just want to return "nothing", so that the table view won't jump to any section. I tried to return nil, but I get the "Return makes integer from pointer without a cast" - warning, since NSInteger is obviously not an object, just a typedef for 32/64 bit integers.

在某些项目(即部分索引标题)上,我只想返回“无”,以便表视图不会跳转到任何部分。我试图返回 nil,但我得到“返回从指针生成整数而不进行强制转换” - 警告,因为 NSInteger 显然不是一个对象,只是 32/64 位整数的 typedef。

How can I achieve no return / reaction on specific section index titles?

如何在特定部分索引标题上实现不返回/反应?

回答by Quinn Taylor

Returning nilis an error here since you're returning an integer primitive, not an object. (You're getting a cast warning because nilis actually a #definethat evaluates to ((void *)0), which is a null pointer, not an integer zero.) The best option for Objective-C code that interfaces with Cocoa is probably to use NSNotFound, a #definefor NSIntegerMaxwhich is used throughout Cocoa to signify that a given value does not exist in the receiver, etc. (Another option is to use -1, which is more common in C code. What works best depends on what the calling code expects and can handle.)

nil在这里返回是一个错误,因为您返回的是一个整数原语,而不是一个对象。(你得到的铸警告,因为nil实际上是一个#define计算结果为((void *)0),这是一个空指针,而不是一个整数零)的Objective-C代码的最佳选择,与可可接口可能是使用NSNotFound,一个#define用于NSIntegerMax使用哪个在 Cocoa 中,用于表示接收器中不存在给定值等。(另一种选择是使用-1,这在 C 代码中更常见。什么效果最好取决于调用代码期望和可以处理的内容。)

Although NSNotFoundis a signed value, it's big enough that you're highly unlikely to run into a range issue. (NSIntegerMaxis approximately half of NSUIntegerMax, and very few people get remotely close to 2,147,483,647 objects — let alone twice that many — in 32-bit land. In 64-bit, forget about it; you'll run out of physical RAM in your machine long before you run out of integers for indexes.)

虽然NSNotFound是有符号值,但它足够大,您不太可能遇到范围问题。(NSIntegerMax大约是 的一半NSUIntegerMax,很少有人能远程接近 2,147,483,647 个对象——更不用说两倍——在 32 位领域。在 64 位中,忘记它;你的机器中的物理 RAM 会用完很长时间在用完索引的整数之前。)

Speaking of which, the Cocoa convention is to use NSUInteger(rather than NSInteger) for indexes. An unsigned integer cannot be negative, which offers somesanity protection on index values; among other things, it becomes easier to sort out accidental integer overflow/underflow. If this is a custom data source method (as it seems to be) I'd strongly suggest switching to using unsigned integers. (It may help to remember/realize that NSIntegerand NSUIntegeroccupy the same number of bytes, they just interpret the bits differently, so you won't "waste" any space by switching types.)

说到这里,Cocoa 约定是使用NSUInteger(而不是NSInteger)作为索引。无符号整数不能为负数,这为索引值提供了一些完整性保护;除此之外,整理意外的整数溢出/下溢变得更容易。如果这是一个自定义数据源方法(看起来是这样),我强烈建议改用无符号整数。(记住/意识到这一点NSIntegerNSUInteger占用相同数量的字节可能会有所帮助,它们只是以不同的方式解释位,因此您不会通过切换类型“浪费”任何空间。)