ios - NSMakeRange导致应用程序锁定;表现慢

标签 ios objective-c uitableview nsstring nsrange

我有一个困境,我认为到目前为止我能解决的很好。但是,当我使用UITableView测试它时,会收到各种警告,例如:

[__NSCFString substringWithRange:]: Range {2147483647, 2147483701} out of bounds; string length 56. This will become an exception for apps linked after 10.9. Warning shown once per app execution.

在下面发布方法之前,我将描述我要做的事情。基本上,当用户上传个人资料照片时,我在其用户名之后但在文件扩展名(.png)之前设置了唯一的子字符串,例如:JohnDoe的用户名拍摄了新的个人资料照片,我将文件名设置为:
NSString *file = [NSString stringWithFormat:@"JohnDoe%@.png",[[NSProcessInfo processInfo] globallyUniqueString]];

我这样做是为了让应用程序可以稍后查看是否需要下载用户 friend 的新个人资料图片,这是基于查看此完整的NSString是否保存在本地磁盘上,如果我找不到它保存在本地磁盘上,我只需从网络。问题很可能来自UITableView连续调用该方法。

另外,这需要在客户端完成,因此请不要讨论与后端相关的解决方案。

这是我想出的用于获取用户个人资料图像文件名的实际唯一NSString的方法。我的这种方法在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中调用
- (NSString *)actualPhotoPath:(NSDictionary *)input
{
    // This code needs work, ridiculous bottle neck!
    NSRange r1 = [[input objectForKey:@"pic"] rangeOfString:[input objectForKey:@"username"]];
    NSRange r2 = [[input objectForKey:@"pic"] rangeOfString:@".png"];
    NSRange rSub = NSMakeRange(r1.location + r1.length, r2.location - r1.location - r1.length);
    // Hangs up on the below line of code eventually
    NSString *sub = [[input objectForKey:@"pic"] substringWithRange:rSub];
    NSString *actual = [NSString stringWithFormat:@"%@%@.png",[input objectForKey:@"username"],sub];
    NSLog(@"%@",actual);
    return actual;
}

感谢您提供的任何帮助!谢谢

最佳答案

您应该检查if(r1.location != NSNotFound)if(r2.location != NSNotFound)214748364710 == 0x7FFFFFFF16等于231-1,它是32位系统上的最高NSInteger值,也称为NSIntegerMax
看看docs:

enum {
   NSNotFound = NSIntegerMax
};
- (NSString *)actualPhotoPath:(NSDictionary *)input
{
    NSRange r1 = [[input objectForKey:@"pic"] rangeOfString:[input objectForKey:@"username"]];
    NSRange r2 = [[input objectForKey:@"pic"] rangeOfString:@".png"];

    if(r1.location != NSNotFound && r2.location != NSNotFound){
        NSRange rSub = NSMakeRange(r1.location + r1.length, r2.location - r1.location - r1.length);
    
        NSString *sub = [[input objectForKey:@"pic"] substringWithRange:rSub];
        NSString *actual = [NSString stringWithFormat:@"%@%@.png",[input objectForKey:@"username"],sub];
        NSLog(@"%@",actual);
        return actual;
    }

    // if the code reaches this block, one of the ranges didn't work.
    // do what ever must be done in that case here
    return nil;
}

实际上,您首先应该创建r1,如果location!= NSFound,则应该使用r2的位置来确保在其后找到字符串png。
- (NSString *)actualPhotoPath:(NSDictionary *)input
{
    NSRange r1 = [[input objectForKey:@"pic"] rangeOfString:[input objectForKey:@"username"]];
    if (r1.location != NSNotFound ) {
        NSRange r2 = [[[input objectForKey:@"pic"] substringFromIndex:r1.location] rangeOfString:@".png"];
        
        if(r2.location != NSNotFound){
            NSRange rSub = NSMakeRange(r1.location + r1.length, r2.location - r1.location - r1.length);
            
            NSString *sub = [[input objectForKey:@"pic"] substringWithRange:rSub];
            NSString *actual = [NSString stringWithFormat:@"%@%@.png",[input objectForKey:@"username"],sub];
            NSLog(@"%@",actual);
            return actual;
        }
    }
    return nil;
}

或者,您可以使用NSRegularExpressions或NSScanner。

关于ios - NSMakeRange导致应用程序锁定;表现慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23975982/

相关文章:

iphone - 如何截取整个 View 的 iPhone 屏幕截图,包括屏幕外的部分?

ios - Swift 4 Decodable 解析数组中的 json 数据

ios - 切换 View Controller 后第二次调用方法的问题

objective-c - Objective C 运行时如何查找已定义的消息?

ios - Table view cell height based on Dynamic height Table Height问题

ios - UITableView 延迟 reloadData 直到动画完成

iphone - iOS:在 iOS 中处理数据库的最佳方式是什么?

iphone - 为什么即使我覆盖 touchesMoved :withEvent:? UIScrollView 的子类仍然滚动

objective-c - 数组未到达 cellForRowAtIndexPath

ios - 如何动态改变iOS应用程序中的标签数量?