ios之NSMakeRange导致应用程序锁定;表现慢
jiqing9006
阅读:92
2025-06-02 22:19:02
评论:0
我有一个困境,我认为到目前为止我能解决的很好。但是,当我使用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。
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。



