简体   繁体   English

NSRange给出了荒谬的位置

[英]NSRange giving nonsensical location

I'm experiencing this weird effect where calling location on rangeOfString is returning a very large value when it shouldn't... 我遇到这种奇怪的效果,在rangeOfString上调用location时它应该返回一个非常大的值,当它不应该...

my code looks like this: 我的代码看起来像这样:

NSRange range = [string rangeOfString:subString];
NSLog(@"range is %@", NSStringFromRange(range));

this logs out to range is {2147483647, 0} 这个注销range is {2147483647, 0}

Can't figure out why exactly. 无法弄清楚为什么。

Some things that might be causing this: 可能导致此问题的一些事情:

  • The substring is generated from an array of bytes using NSString's initWithBytes method 子字符串是使用NSString的initWithBytes方法从字节数组生成的
  • The substring tells me its an NSCFString * but also says 'variable is not NSString' 子字符串告诉我它是一个NSCFString *但是也说'变量不是NSString'
  • the substring logs in the console as expected 子字符串按预期在控制台中记录

any help greatly appreciated 任何帮助非常感谢

here is somethign that is striking me as weird: 这是一些让我感到奇怪的东西:

在此输入图像描述

That just means that your substring is not present in your string, do the following to verify this: 这只是意味着您的子字符串不存在于您的字符串中,请执行以下操作来验证这一点:

NSRange range = [string rangeOfString:subString];
if (range.location == NSNotFound) {
  NSLog(@"String not found");
} else {
  NSLog(@"range is %@", NSStringFromRange(range));
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM