简体   繁体   English

NSRange:range.location!= NSNotFound vs. range.length> 0

[英]NSRange: range.location != NSNotFound vs. range.length > 0

I'm going through some older code in one of my apps and fixing up the code in areas that could be potentially problematic. 我正在浏览我的一个应用程序中的一些旧代码,并在可能存在问题的领域修复代码。

I'm seeing a lot of old code using... 我看到很多旧代码使用...

NSRange range = //determine range here....
if(range.length > 0)
{
    //do stuff
}

Is that code "fine", or should I change it to this? 该代码是“罚款”,还是应该将其改为此?

NSRange range = //determine range here....
if(range.location != NSNotFound)
{
    //do stuff
}

Are these two methods identical, essentially, or not? 这两种方法基本上是否相同?

The two checks are not always identical. 这两个检查并不总是相同的。 It depends on how the range was generated. 这取决于范围的生成方式。 Example: 例:

NSRegularExpression *re = [NSRegularExpression
    regularExpressionWithPattern:@"(?= )" options:0 error:NULL];
NSTextCheckingResult *result = [re firstMatchInString:@"hello world"
    options:0 range:NSMakeRange(0, 11)];
NSLog(@"range = %@", NSStringFromRange(result.range));

The range's length is 0, but its location is 5, not NSNotFound . 范围的长度为0,但其位置为5,而不是NSNotFound

The answer depends on the function/method you are using. 答案取决于您使用的功能/方法。 NSRange is just a struct so you need to read the documentation for the function/method you are calling. NSRange只是一个结构,因此您需要阅读您正在调用的函数/方法的文档。

Examples: 例子:

NSRangeFromString NSRangeFromString
Returns a range from a textual representation. 返回文本表示的范围。

... If aString does not contain any integers, this function returns an NSRange struct whose location and length values are both 0. ...如果aString不包含任何整数,则此函数返回NSRange结构,其位置和长度值均为0。

In this case checking for NSNotFound would not work. 在这种情况下,检查NSNotFound将不起作用。


-[NSString rangeOfString:] - [NSString rangeOfString:]

... Returns {NSNotFound, 0} if aString is not found or is empty (@""). ...如果找不到aString或为空(@“”),则返回{NSNotFound,0}。

Here it is documented that location will be NSNotFound and the length will be 0 so either of the checks would work, however, I would recommend checking the location against NSNotFound . 这里记录了位置将是NSNotFoundlength将为0,因此任何一个检查都可以,但是,我建议根据NSNotFound检查位置。

NSNotFound is defined as "NSIntegerMax". NSNotFound定义为“NSIntegerMax”。 Even if they produce the same result your second display is far more readable and self documenting. 即使它们产生相同的结果,您的第二个显示器也更具可读性和自我记录性。 Maybe you shouldn't find them all and change them but just switch to that moving forward. 也许你不应该找到它们并改变它们,但只需转向前进即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有当 range.location &gt; targetString.length, range.length !=0 的情况 - Is there a situation that when range.location > targetString.length, range.length !=0 为什么rangeChangeCharactersInRange委托方法中的退格的range.length值会发生变化? - Why range.length value changes for backspace in shouldChangeCharactersInRange delegate methos? NSRange与NSNotFound不匹配 - NSRange not matching with NSNotFound 来自 Swift Range 的 NSRange? - NSRange from Swift Range? 在NSAttributedString API中使用Range <Index>和NSRange - Using Range<Index> with NSRange in the NSAttributedString API 无法转换&#39;Range&#39;类型的值 <String.Index> ? 到指定的类型“ NSRange”(又名“ _NSRange”) - Cannot convert value of type 'Range<String.Index>?' to specified type 'NSRange' (aka '_NSRange') 无法转换&#39;Range类型的值 <String.Index> &#39;(又名&#39;范围 <String.CharacterView.Index> &#39;)预期参数类型&#39;NSRange&#39;(又名&#39;_NSRange&#39;) - Cannot convert value of type 'Range<String.Index>' (aka 'Range<String.CharacterView.Index>') to expected argument type 'NSRange' (aka '_NSRange') 创建NSRange的NSMutableArray,并在以后正确读取范围值 - Create an NSMutableArray of NSRange's and properly read the range values later 无法将&#39;NSRange&#39;(又名&#39;_NSRange&#39;)类型的值转换为预期类型&#39;Range <Index> &#39;(又名&#39;范围 <String.CharacterView.Index> “) - Cannot convert value of type 'NSRange' (aka '_NSRange') to expected type 'Range<Index>'(aka 'Range<String.CharacterView.Index>') NSRange给出了荒谬的位置 - NSRange giving nonsensical location
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM