我完全迷失了方向,甚至不确定如何表达这个问题。我有一个循环,如下所示。如果if
循环中的语句证明为trueindexPath
分配了值,则循环完成(_samplePicture
s数组中有49个对象)if (found)
,然后输入,然后加载找到的数据。如果条件不成立,并且未找到任何内容而不是进入if(!found)
应用程序,则崩溃。我已经尝试了这种当前编码方案的各种结构。我什至删除了循环,并输入了if(!found)
它应该执行的操作。(ooooodddddddd)
该语句位于的viewDidLoad
正上方(if语句不是viewDidLoad
),它是if
确定的内容的语句_samplePictures
。
知道数组中的图像定义如下可能很有用:
#define IMG_71 [UIImage imageNamed:@"imagename.jpg"]
数组的结构如下:
_samplePictures = @[@{@"Image": IMG_71, @"Title" : @"title of item", @"Description" : @"complete description of item"}];
if (fromKitInstructions)
{
int i = 0;
NSIndexPath *indexPath;
found = FALSE;
while(i <= _samplePictures.count)
{
if([_samplePictures[i] objectForKey:@"Title"] == instructionName)
{
indexPath = [NSIndexPath indexPathForRow:i inSection:0];
i = 50;
found = TRUE;
}
else
{
i++;
}
}
if(found)
{
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
}
if(!found)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:instructionName message:@"We are sorry but the Kit ID you searched for could not be found. Press the Back button to return to the Instructions you were viewing." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Say Hello",nil];
[alert show];
}
}
这是使您进入此代码的按钮:
-(void)goTo:(id)sender
{
ViewController *goGallery = [[ViewController alloc] initWithNibName:@"ViewController" bundle:[NSBundle mainBundle]];
goGallery.setFlag = NO;
goGallery.fromKitInstructions = YES;
goGallery.instructionName = instructionName;
[self.navigationController pushViewController:goGallery animated:YES];
}
调试器显示以下内容:
2014-05-30 15:08:37.873 TechBook[3932:60b] [INFO] <HomeViewController: 0xc67bb00> loaded
2014-05-30 15:14:24.268 TechBook[3932:3c03] void SendDelegateMessage(NSInvocation *): delegate (webView:decidePolicyForMIMEType:request:frame:decisionListener:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode
2014-05-30 15:14:43.204 TechBook[3932:60b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 49 beyond bounds [0 .. 48]'
*** First throw call stack:
(
0 CoreFoundation 0x01f3d1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01cbc8e5 objc_exception_throw + 44
2 CoreFoundation 0x01ef18b2 -[__NSArrayI objectAtIndex:] + 210
3 CoreFoundation 0x01fbbf48 -[NSArray objectAtIndexedSubscript:] + 40
4 TechBook 0x0000ae3d -[ViewController viewDidLoad] + 22381
5 UIKit 0x0088e33d -[UIViewController loadViewIfRequired] + 696
6 UIKit 0x0088e5d9 -[UIViewController view] + 35
7 UIKit 0x008a8942 -[UINavigationController _startCustomTransition:] + 778
8 UIKit 0x008b58f7 -[UINavigationController _startDeferredTransitionIfNeeded:] + 688
9 UIKit 0x008b64e9 -[UINavigationController __viewWillLayoutSubviews] + 57
10 UIKit 0x009f70d1 -[UILayoutContainerView layoutSubviews] + 213
11 UIKit 0x007de964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
12 libobjc.A.dylib 0x01cce82b -[NSObject performSelector:withObject:] + 70
13 QuartzCore 0x001f145a -[CALayer layoutSublayers] + 148
14 QuartzCore 0x001e5244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
15 QuartzCore 0x001e50b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
16 QuartzCore 0x0014b7fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
17 QuartzCore 0x0014cb85 _ZN2CA11Transaction6commitEv + 393
18 QuartzCore 0x0020a5b0 +[CATransaction flush] + 52
19 UIKit 0x0076d9bb _UIApplicationHandleEventQueue + 13095
20 CoreFoundation 0x01ec677f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
21 CoreFoundation 0x01ec610b __CFRunLoopDoSources0 + 235
22 CoreFoundation 0x01ee31ae __CFRunLoopRun + 910
23 CoreFoundation 0x01ee29d3 CFRunLoopRunSpecific + 467
24 CoreFoundation 0x01ee27eb CFRunLoopRunInMode + 123
25 GraphicsServices 0x036d75ee GSEventRunModal + 192
26 GraphicsServices 0x036d742b GSEventRun + 104
27 UIKit 0x0076ff9b UIApplicationMain + 1225
28 TechBook 0x00003b1d main + 141
29 libdyld.dylib 0x027ee701 start + 1
30 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
while(i <= _samplePictures.count)
更改为:
while(i < _samplePictures.count)
数组从0-48运行,但_sampePictures.count
返回总共49个对象。因此,当您遍历数组的最后一次运行时,计数总数大于最终索引。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句