ドキュメントディレクトリにあるファイルのファイル名をテーブルビューに読み込もうとしていますが、ボタンをクリックしてテーブルビューを表示すると、インデックスのオブジェクトが0であるが、配列内には14個のオブジェクトが存在するというエラーが発生します。何が原因だろう?TableViewにドキュメントディレクトリのファイルの名前を設定する空の配列を持つ例外
@implementation loadDataTVC
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void) viewWillAppear:(BOOL)animated{
DataSource *sharedManager = [DataSource sharedManager];
NSString *documentDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
sharedManager.directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirectoryPath error:NULL];
NSLog(@"%lu", (unsigned long)[sharedManager.directoryContent count]);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
DataSource *sharedManager = [DataSource sharedManager];
NSString *documentDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
sharedManager.directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirectoryPath error:NULL];
if (sharedManager.directoryContent.count > 0) {
return (unsigned long)[sharedManager.directoryContent count];
}
else{
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"files" forIndexPath:indexPath];
DataSource *sharedManager = [DataSource sharedManager];
NSString *documentDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
sharedManager.directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirectoryPath error:NULL];
if (sharedManager.directoryContent.count > 0) {
cell.textLabel.text = [NSString stringWithFormat:@"%@", [sharedManager.directoryContent objectAtIndex:indexPath.row]];
}
else{
cell.textLabel.text = @"No data Available";
}
return cell;
}
ログ:
2016-05-18 15:00:44.502 GeoData Grapher[4554:331076] 14
2016-05-18 15:00:47.412 GeoData Grapher[4554:331076] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray'
*** First throw call stack:
(
0 CoreFoundation 0x000000010d2cdd85 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010cd41deb objc_exception_throw + 48
2 CoreFoundation 0x000000010d275885 -[__NSArray0 objectAtIndex:] + 101
3 GeoData Grapher 0x000000010a5f07d6 -[DeviceListTVC prepareForSegue:sender:] + 326
4 UIKit 0x000000010bcc95d5 -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 369
5 UIKit 0x000000010bcc9433 -[UIStoryboardSegueTemplate _perform:] + 82
6 UIKit 0x000000010bcc96f7 -[UIStoryboardSegueTemplate perform:] + 156
7 UIKit 0x000000010b583a8d -[UIApplication sendAction:to:from:forEvent:] + 92
8 UIKit 0x000000010b991067 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 152
9 UIKit 0x000000010b583a8d -[UIApplication sendAction:to:from:forEvent:] + 92
10 UIKit 0x000000010b6f6e67 -[UIControl sendAction:to:forEvent:] + 67
11 UIKit 0x000000010b6f7143 -[UIControl _sendActionsForEvents:withEvent:] + 327
12 UIKit 0x000000010b6f72be -[UIControl _sendActionsForEvents:withEvent:] + 706
13 UIKit 0x000000010b6f6263 -[UIControl touchesEnded:withEvent:] + 601
14 UIKit 0x000000010b5f699f -[UIWindow _sendTouchesForEvent:] + 835
15 UIKit 0x000000010b5f76d4 -[UIWindow sendEvent:] + 865
16 UIKit 0x000000010b5a2dc6 -[UIApplication sendEvent:] + 263
17 UIKit 0x000000010b57c553 _UIApplicationHandleEventQueue + 6660
18 CoreFoundation 0x000000010d1f3301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
19 CoreFoundation 0x000000010d1e922c __CFRunLoopDoSources0 + 556
20 CoreFoundation 0x000000010d1e86e3 __CFRunLoopRun + 867
21 CoreFoundation 0x000000010d1e80f8 CFRunLoopRunSpecific + 488
22 GraphicsServices 0x000000010eb7aad2 GSEventRunModal + 161
23 UIKit 0x000000010b581f09 UIApplicationMain + 171
24 GeoData Grapher 0x000000010a5f405f main + 111
25 libdyld.dylib 0x000000011253c92d start + 1
26 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
セグエ:
これは、配列がnilであることを意味します。ブレークポイントを設定するか、nslogを使用してチェックしてください。 – Lion
'3 GeoData Grapher 0x000000010a5f07d6 - [DeviceListTVC prepareForSegue:sender:] + 326'が問題と思われます - DecipeListTVCの' prepareForSegue'ではどうしますか? – luk2302
@Lion NSlogの結果が上部(14)に表示されます。したがって、配列は空ではありません – Wilbe