为什么NSNotFound不是-1而是NSIntegerMax [英] Why NSNotFound isn't -1 but NSIntegerMax

查看:451
本文介绍了为什么NSNotFound不是-1而是NSIntegerMax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSNotFound定义为NSIntegerMax,它在32位和64位上具有不同的值,这给持久和分布式环境带来了很多不便.为什么不将其定义为-1?

NSNotFound is defined as NSIntegerMax, which has different values on 32-bit and 64-bit, this brings a lot of inconvenience for persistent and distributed env. Why not define it as -1?

PS,在ObjC和Cocoa中,某些indexOf ...方法返回NSNotFound但某些返回-1,是有原因的,这些结果不一致.

PS, In ObjC and Cocoa, some indexOf... methods returns NSNotFound but some returns -1, is there a reason these results are inconsistent.

推荐答案

好答案可能不是,因为有时它会以无符号值形式返回-在Objective-C中,有符号和无符号之间的转换实际上是位副本,因此您可以将无符号数与-1比较并得到您期望的答案(值全为--1为有符号,最大整数为无符号).请参阅对该问题的答案.

Well the answer probably isn't because it is sometimes returned as an unsigned value - in Objective-C conversions between signed and unsigned are effectively bit-copies so you can compare an unsigned to -1 and get the answer you expect (the values is all ones - -1 as signed, maximum integer as unsigned). See answer to this question.

因此,我们进入您问题的第二部分,为什么会出现不一致?好吧,多样性是生活的香料,或者换句话说,现在比民间的还要奇怪-人们只是前后矛盾,没有比这更复杂了!

So we come to your second part of the question, why in the inconsistency? Well variety is the spice of life, or put another way there's nowt so queer as folk - people are just inconsistent, no more complicated than that!

这篇关于为什么NSNotFound不是-1而是NSIntegerMax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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