为什么NSIndexPath的row属性是有符号整数? [英] Why is the row property of NSIndexPath a signed integer?

查看:148
本文介绍了为什么NSIndexPath的row属性是有符号整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么NSIndexPath的row属性是有符号整数?

Why is the row property of NSIndexPath a signed integer?

是否可以使用有效负值?

Could it ever take on a "valid" negative value?

编辑

直到今天我设置LLVM来检查符号比较时才考虑这个问题。这使编译器在 indexPath.row< = [someArray count] 或类似的情况下发出警告。

I haven't thought about this until today when I set LLVM to check sign comparison. This made the compiler spew out warnings whenever there was indexPath.row <= [someArray count] or similar.

推荐答案

如果使用负数会怎样?



使用负值是不明智的,如果你这样做,你呢?会得到疯狂的结果

What happens if you use negative numbers?

It isn't wise to use negative values, if you do, you'll get crazy results

NSIndexPath* path = [NSIndexPath indexPathForRow:-2 inSection:0];

上面的结果是0的一部分和一行的4294967294(看起来像是下溢的 NSUInteger 给我!)安全地知道这只发生在UIKit Additions类别中,而不是在NSIndexPath本身内。看看 NSIndexPath 背后的概念,保持负值真的没有意义。那么为什么呢?

The above results in a section of 0, and a row of 4294967294 (which looks like underflow of an NSUInteger to me!) Be safe in the knowledge that this only occurs within the UIKit Additions category, and not within NSIndexPath itself. Looking at the concept behind NSIndexPath, it really doesn't make sense to hold negative values. So why?

核心对象 NSIndexPath 使用 NSUInteger s作为其索引,但UIKit Addition使用 NSInteger 。该类别仅建立在核心对象之上,但 NSInteger 超过 NSUInteger 的使用不提供任何额外的能力。

The core object NSIndexPath from OS X uses NSUIntegers for its indices, but the UIKit Addition uses NSInteger. The category only builds on top of the core object, but the use of NSInteger over NSUInteger doesn't provide any extra capabilities.

为什么它这样工作,我不知道。我的猜测(我规定 guess ),是第一次启动iOS时它是一个天真的API漏洞。当在iOS 2中发布 UITableView 时,它使用 NSInteger 来处理各种事情(例如 numberOfSections )。想一想:从概念上讲这没有意义,你不能有负数的部分。现在即使在iOS 6中,它仍然使用 NSInteger ,因此不要破坏以前的应用程序与表视图的兼容性。

Why it works this way, I have no idea. My guess (and I stipulate guess), is it was a naive API slipup when first launching iOS. When UITableView was released in iOS 2, it used NSIntegers for a variety of things (such as numberOfSections). Think about it: This conceptually doesn't make sense, you can't have a negative number of sections. Now even in iOS 6, it still uses NSInteger, so not to break previous application compatibility with table views.

除了 UITableView 之外,我们还添加了 NSIndexPath ,它们与表视图一起用于访问它的行和这样。因为它们必须一起工作,所以它们需要兼容类型(在这种情况下 NSInteger )。

Alongside UITableView, we have the additions to NSIndexPath, which are used in conjunction with the table view for accessing it's rows and such. Because they have to work together, they need compatible types (in this case NSInteger).

更改类型全面的 NSUInteger 会破坏很多东西,为了安全的API设计,一切都需要重命名,以便NSInteger和NSUInteger对应的工作可以安全地并排工作。 Apple可能不希望这样麻烦(开发人员也不想这样做),因此他们将它保存到 NSInteger

To change the type to NSUInteger across the board would break a lot of things, and for safe API design, everything would need to be renamed so that the NSInteger and NSUInteger counterparts could work safely side by side. Apple probably don't want this hassle (and neither do the developers!), and as such they have kept it to NSInteger.

这篇关于为什么NSIndexPath的row属性是有符号整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆