NSRangeException 混乱

发布于 2024-12-28 02:30:33 字数 3103 浏览 4 评论 0原文

我连接了一个 UISearchBar 来在键入文本时重新加载 AQGridView 。但是,令人惊讶的是,在 NSRangeException 终止我的应用程序之前,可以在搜索栏中输入的字符数是有限制的。在搜索栏中输入第 11 个字符时,会出现 NSRangeException 并显示以下消息:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSPathStore2 getCharacters:range:]: index (11) beyond bounds (11)'
*** First throw call stack:
(0x261b052 0x27acd0a 0x25c3a78 0x25c39e9 0x1aded12 0x252498e 0x25369b3 0x1ae020b 0x1ae00f0 0x2a367 0x14204b2 0x261cec9 0x11ed515 0x129372f 0x1292e1e 0x129fce9 0x12ac12a 0x1b9ea39 0x25e6885 0x25e67a8 0x1ae31aa 0x6f6b8e7 0x6376917 0x673a111 0x673d4e1 0x6f4685b 0x6f492e3 0x6f49440 0x6f4a09f 0x674584d 0x6745b32 0x6759e12 0x6cbf0f7 0x6758245 0x67571f2 0x67578fb 0x6cbeca4 0x676c64e 0x675a0a0 0x673ca0a 0x63a7ad9 0x261ce72 0x6f665bc 0x63ce7f9 0x63d087f 0x138ae03 0x134fb9b 0x134f62b 0x134e6b6 0x1357f09 0x11f9406 0x12d714a 0x12d714a 0x12d714a 0x12d714a 0x12d714a 0x12d714a 0x12d714a 0x12d714a 0x12d714a 0x12d714a 0x12d714a 0x11f9460 0x11f90c5 0x11f91f8 0x11ecaa9 0x29aefa9 0x25ef1c5 0x2554022 0x255290a 0x2551db4 0x2551ccb 0x29ad879 0x29ad93e 0x11eaa9b 0x20ed 0x2065)
terminate called throwing an exception 

该错误显然来自 UISearchBar 委托方法 -(void)searchBar:(UISearchBar * )searchBar textDidChange:(NSString *)searchText,所以这是整个方法:

更新1:新代码。如果文本比产品名称长,我只是打破了 for in 循环,但这并不能解决问题,这只是一种解决方法。

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    /*
     Update the filtered array based on the search text and scope.
     */
    [copyListOfItems removeAllObjects]; // First clear the filtered array.
    /*
     Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
     */

    if (!searchText.length) {
        isSearching = NO;
        [filterControl setSelectedSegmentIndex:0];
        [filterControl setEnabled:YES];
        [self.gridView reloadData];
    }
    else {
        [filterControl setSelectedSegmentIndex:0];
        [filterControl setEnabled:NO];
        isSearching = YES;
        for (NSString *product in _documentIconsURLs)
        {
            if (searchText.length >= [[product lastPathComponent]length])
                break;

            NSComparisonResult result = [[product lastPathComponent] compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
            if (result == NSOrderedSame)
                {
                    [copyListOfItems addObject:[product lastPathComponent]];
                    [copyListOfItems sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];
                }
            }
        [self.gridView reloadData];
    }
} 

全局异常断点表明整个代码的这一部分正在抛出异常:

NSComparisonResult result = [[product lastPathComponent] compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];

所以我的问题是,为什么 11 个字符会抛出范围异常?这可以补救吗?

I have a UISearchBar hooked up to reload an AQGridView as text is typed. But, amazingly, there is a limit on the number of characters that can possibly be entered into the search bar until an NSRangeExceptionkills my app. Upon the entering of the 11th character entered into the search bar, an NSRangeException crops up with this message:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSPathStore2 getCharacters:range:]: index (11) beyond bounds (11)'
*** First throw call stack:
(0x261b052 0x27acd0a 0x25c3a78 0x25c39e9 0x1aded12 0x252498e 0x25369b3 0x1ae020b 0x1ae00f0 0x2a367 0x14204b2 0x261cec9 0x11ed515 0x129372f 0x1292e1e 0x129fce9 0x12ac12a 0x1b9ea39 0x25e6885 0x25e67a8 0x1ae31aa 0x6f6b8e7 0x6376917 0x673a111 0x673d4e1 0x6f4685b 0x6f492e3 0x6f49440 0x6f4a09f 0x674584d 0x6745b32 0x6759e12 0x6cbf0f7 0x6758245 0x67571f2 0x67578fb 0x6cbeca4 0x676c64e 0x675a0a0 0x673ca0a 0x63a7ad9 0x261ce72 0x6f665bc 0x63ce7f9 0x63d087f 0x138ae03 0x134fb9b 0x134f62b 0x134e6b6 0x1357f09 0x11f9406 0x12d714a 0x12d714a 0x12d714a 0x12d714a 0x12d714a 0x12d714a 0x12d714a 0x12d714a 0x12d714a 0x12d714a 0x12d714a 0x11f9460 0x11f90c5 0x11f91f8 0x11ecaa9 0x29aefa9 0x25ef1c5 0x2554022 0x255290a 0x2551db4 0x2551ccb 0x29ad879 0x29ad93e 0x11eaa9b 0x20ed 0x2065)
terminate called throwing an exception 

That error is obviously coming from the UISearchBar Delegate Method -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText, so here's the whole method:

UPDATE 1: New code. I just broke the for in loop if the text was longer than the product name, but this doesn't fix a thing, it';s just a workaround.

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    /*
     Update the filtered array based on the search text and scope.
     */
    [copyListOfItems removeAllObjects]; // First clear the filtered array.
    /*
     Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
     */

    if (!searchText.length) {
        isSearching = NO;
        [filterControl setSelectedSegmentIndex:0];
        [filterControl setEnabled:YES];
        [self.gridView reloadData];
    }
    else {
        [filterControl setSelectedSegmentIndex:0];
        [filterControl setEnabled:NO];
        isSearching = YES;
        for (NSString *product in _documentIconsURLs)
        {
            if (searchText.length >= [[product lastPathComponent]length])
                break;

            NSComparisonResult result = [[product lastPathComponent] compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
            if (result == NSOrderedSame)
                {
                    [copyListOfItems addObject:[product lastPathComponent]];
                    [copyListOfItems sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];
                }
            }
        [self.gridView reloadData];
    }
} 

A global exception breakpoint reveals that this section of the whole code is throwing the exception:

NSComparisonResult result = [[product lastPathComponent] compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];

So my question is, why would 11 characters throw a range exception? Can this be remedied?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰雪之触 2025-01-04 02:30:33

来自 比较:选项:范围:

重要如果范围超出接收器的边界,则会引发NSRangeException

我的猜测是您的产品之一的 lastPathComponent 少于 11 个字符。试试这个:

NSComparisonResult result = [searchText compare:[product lastPathComponent] options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];

From the documentation of compare:options:range::

Important Raises an NSRangeException if range exceeds the bounds of the receiver.

My guess is one of your products has a lastPathComponent that is shorter than 11 characters. Try this instead:

NSComparisonResult result = [searchText compare:[product lastPathComponent] options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
峩卟喜欢 2025-01-04 02:30:33

好的找到了答案。这是一个 API 问题。 Apple 更改了 64 位上的实现。该代码将在 32 位设备上运行,它可以工作,但不能在 64 位设备上运行。正如我之前所说,放置一个 if else 语句来检查已搜索字符串的大小。

Ok found the answer. This is an API issue. Apple has changed implementation on 64bit. That code will run on a 32bit device it will work but not on a 64bit. As I said before put a if else statement to check the size of the string been searched.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文