[英]What is wrong with this for loop - trying to fill array with Tweet object?
[英]Trying to Append Retweeted Tweet
我正在嘗試在Objective-C中附加“轉發”推文的字符串,但是某些類型的推文始終會遇到問題。 我認為我嘗試對推文進行細分的方式有問題。 我的代碼如下所示
2015-08-20 15:40:20.699 Floadt[21417:932559] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString substringWithRange:]: Range {9223372036854775807, 52} out of bounds; string length 70'
*** First throw call stack:
(
0 CoreFoundation 0x000000010cd58c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010c631bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010cd58b9d +[NSException raise:format:] + 205
3 CoreFoundation 0x000000010cc7fb98 -[__NSCFString substringWithRange:] + 136
4 Floadt 0x000000010929efa4 -[TwitterTableViewController tableView:cellForRowAtIndexPath:] + 2260
5 UIKit 0x000000010b465a28 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
6 UIKit 0x000000010b444248 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2853
7 UIKit 0x000000010b45a8a9 -[UITableView layoutSubviews] + 210
8 UIKit 0x000000010b3e4a2b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
9 QuartzCore 0x000000010b1a6ec2 -[CALayer layoutSublayers] + 146
10 QuartzCore 0x000000010b19b6d6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
11 QuartzCore 0x000000010b19b546 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
12 QuartzCore 0x000000010b107886 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
13 QuartzCore 0x000000010b108a3a _ZN2CA11Transaction6commitEv + 462
14 QuartzCore 0x000000010b1090eb _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
15 CoreFoundation 0x000000010cc8bca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
16 CoreFoundation 0x000000010cc8bc00 __CFRunLoopDoObservers + 368
17 CoreFoundation 0x000000010cc81a33 __CFRunLoopRun + 1123
18 CoreFoundation 0x000000010cc81366 CFRunLoopRunSpecific + 470
19 GraphicsServices 0x000000010eb09a3e GSEventRunModal + 161
20 UIKit 0x000000010b364900 UIApplicationMain + 1282
21 Floadt 0x000000010936574f main + 111
22 libdyld.dylib 0x000000010f32f145 start + 1
23 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
這是我添加字符串的代碼:
if ([self Contains:@"RT" on:[data objectForKey:@"text"]]) {
TwitterRetweetCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"TwitterRetweetCell"];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]];
if (cell == nil) {
cell = [[TwitterRetweetCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TwitterRetweetCell"];
}
// Lookup RT User
NSString *text = [data objectForKey:@"text"];
NSRange start = [text rangeOfString:@"RT @"];
NSRange end = [text rangeOfString:@":"];
NSString *shortString = [text substringWithRange:NSMakeRange(start.location, end.location)];
NSString *evenShorterString = [shortString substringFromIndex:4];
[self lookupTwitterUser:evenShorterString];
//Set username for twitter
[cell.nameLabel setText:evenShorterString];
//Set Retweet Status
NSString *retName = [NSString stringWithFormat:@"Retweeted by %@",data[@"user"][@"screen_name"]];
[cell.retweetedLabel setText:retName];
//Set Profile Pic for Twitter
return cell;
}
兩個范圍:
NSRange start = [text rangeOfString:@"RT @"];
NSRange end = [text rangeOfString:@":"];
您需要檢查是否確實找到了這些子字符串,並且僅在找到它們的情況下使用范圍。 否則,代碼的其余部分將失敗,因為范圍的位置將為NSNotFound,這是崩潰日志中的大數字。 這表明您的代碼遇到了包含“ RT”但不包含“ RT @”的文本。 在嘗試使用開始和結束之前,應執行以下檢查:
if (start.location != NSNotFound && end.location != NSNotFound)
{
// use start and end
}
或if ([self Contains:@"RT" on:[data objectForKey:@"text"]])
是,使用此if條件代替if ([self Contains:@"RT" on:[data objectForKey:@"text"]])
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.