【发布时间】:2014-06-01 01:27:11
【问题描述】:
我有一个困境,我认为到目前为止我已经很好地解决了。但是,当我使用 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]];
我这样做是为了让应用程序稍后可以根据查看这个完整的 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;
}
感谢您提供的任何帮助!谢谢
【问题讨论】:
标签: ios objective-c uitableview nsstring nsrange