NSScanner 的显着行为
我在 NSTextView 中有以下字符串:
荷鲁斯为父亲奥西里斯报仇
为此分配了两个标签,荷鲁斯
和奥西里斯
。我使用 NSScanner 扫描字符串中的两个标签,并在找到时用黄色背景突出显示它们。
代码是:
for (Tag *aTag in tags) {
NSString *aTagName = [aTag name];
NSUInteger strLength = [aTagName length];
NSScanner *aScanner = [[NSScanner alloc] initWithString: aString];
[aScanner setCaseSensitive: YES];
[aScanner setScanLocation: 0];
BOOL result = [aScanner scanUpToString: aTagName intoString: nil];
while (![aScanner isAtEnd]) {
NSUInteger position = [aScanner scanLocation];
if (result) {
NSRange aRange = NSMakeRange(position, strLength);
[storage removeAttribute: NSBackgroundColorAttributeName range: aRange];
if (onOrOff) {
[storage addAttribute: NSBackgroundColorAttributeName value: aColor range: aRange];
}
position = position + [aTagName length];
[aScanner setScanLocation: position];
}
[aScanner scanUpToString: aTagName intoString: nil];
}
}
当第一个标签(在上面的示例中是 Horus
)位于扫描字符串的最开头(位置 = 0)时,此操作会失败。找不到标签,代码一直循环。
但是,当我将字符串替换为:
十年后荷鲁斯为父亲奥西里斯报仇
……一切顺利,我的两个标签都被发现(如预期)并很好地突出显示。
我是否遗漏了什么或者这是 NSScanner 中的错误?
I have the following string in an NSTextView:
Horus avenged his father Osiris
There are two tags assigned to this, Horus
and Osiris
. I use NSScanner to scan the string for both tags and highlight them with a yellow background when found.
The code is:
for (Tag *aTag in tags) {
NSString *aTagName = [aTag name];
NSUInteger strLength = [aTagName length];
NSScanner *aScanner = [[NSScanner alloc] initWithString: aString];
[aScanner setCaseSensitive: YES];
[aScanner setScanLocation: 0];
BOOL result = [aScanner scanUpToString: aTagName intoString: nil];
while (![aScanner isAtEnd]) {
NSUInteger position = [aScanner scanLocation];
if (result) {
NSRange aRange = NSMakeRange(position, strLength);
[storage removeAttribute: NSBackgroundColorAttributeName range: aRange];
if (onOrOff) {
[storage addAttribute: NSBackgroundColorAttributeName value: aColor range: aRange];
}
position = position + [aTagName length];
[aScanner setScanLocation: position];
}
[aScanner scanUpToString: aTagName intoString: nil];
}
}
This fails when the frist tag (in the above example it's Horus
) is located at the very beginning (location = 0) of the scanned string. The tag is not found and the code keeps looping.
However, when I replace the string with:
After 10 years Horus avenged his father Osiris
... it all works and both my tags are found (as expected) and nicely highlighted.
Am I missing something or is this a bug in NSScanner?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅文档
扫描器已找到该字符串,但返回“否”,因为它尚未扫描任何字符来执行此操作。然后,您的代码不会增加
position
变量,因为该部分位于if (result)
块内。我认为删除
if
语句应该可以解决问题。如果扫描器没有找到 stopString,则while
条件将失败,并且代码根本不会进入循环。See the docs for
The scanner has found the string, but returned NO, because it has not scanned through any characters to do so. Your code then doesn't increase the
position
variable, because that part is within theif (result)
block.I think removing the
if
statement should fix the problem. If the scanner didn't find the stopString, thewhile
condition will fail and the code won't get into the loop at all.