I have searched through a number of these NSRangeException
error threads but can't seem to find an answer to my question. Here is my error:
2015-08-15 17:28:43.793 UTK Recruiting[8794:254203] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]' *** First throw call stack:
Here is a couple of bits of code. Basically I am going through the documents directories, getting all the filenames and paths, and then I'm trying to populate UITableView
with the filenames so a cell can be selected, and then that file can be attached to an email. Here are a few bits of my code:
Parses documents directory, puts all csv files in corresponding arrays.
- (void) refreshTable {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *documentArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSArray *csvFiles = [documentArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString *evaluatedObject, NSDictionary *bindings) {
return [evaluatedObject hasSuffix:@".csv"];
}]];
self.csvFileNames = (NSMutableArray*) csvFiles;
self.csvFilePaths = [NSMutableArray arrayWithCapacity:[csvFiles count]];
for (NSString *fileName in csvFiles) {
[self.csvFilePaths addObject:[documentsDirectory stringByAppendingPathComponent:fileName]];
}
NSLog(@"files array %@", _csvFileNames);
NSLog(@"files array %@", _csvFilePaths);
This is the UITableView code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSLog(@"count: %lu", (unsigned long)[self.csvFileNames count]);
return [self.csvFileNames count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellforRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text=[self.csvFileNames objectAtIndex:indexPath.row];
return cell;
}
After placing break points throughout my code, I've found that it crashes in the cellForRowAtIndexPath
method, which is the last method in the code I posted. Something else important to note is that there are 4 csv files, and [self.csvFileNames count]
correctly gives the number 4.
The code should be running until index 3, but I can't figure out why it is not.
- thread #1: tid = 0x40646, 0x000000010de9f286 libsystem_kernel.dylib
__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT frame #0: 0x000000010de9f286 libsystem_kernel.dylib
__pthread_kill + 10 frame #1: 0x000000010ded242f libsystem_pthread.dylibpthread_kill + 90 frame #2: 0x000000010dc3f19a libsystem_sim_c.dylib
abort + 129 frame #3: 0x000000010da2a481 libc++abi.dylibabort_message + 257 frame #4: 0x000000010da523d5 libc++abi.dylib
default_terminate_handler() + 267 frame #5: 0x0000000106aeae19 libobjc.A.dylib_objc_terminate() + 103 frame #6: 0x000000010da4fb01 libc++abi.dylib
std::__terminate(void (*)()) + 8 frame #7: 0x000000010da4f7aa libc++abi.dylib__cxa_rethrow + 99 frame #8: 0x0000000106aead2c libobjc.A.dylib
objc_exception_rethrow + 40 frame #9: 0x0000000106d7a41e CoreFoundationCFRunLoopRunSpecific + 654 frame #10: 0x0000000109b13a3e GraphicsServices
GSEventRunModal + 161 frame #11: 0x000000010724a8c0 UIKit`UIApplicationMain + 1282
- frame #12: 0x000000010640211f UTK Recruiting
main(argc=1, argv=0x00007fff598014f8) + 111 at main.m:14 frame #13: 0x000000010db97145 libdyld.dylib
start + 1 frame #14: 0x000000010db97145 libdyld.dylib`start + 1
bt all crash report:
thread #1: tid = 0x56469, 0x0000000109200286 >libsystem_kernel.dylib
__pthread_kill + 10, queue = 'com.apple.main-thread', stop >reason = signal SIGABRT frame #0: 0x0000000109200286 libsystem_kernel.dylib
__pthread_kill + 10 frame #1: 0x000000010923342f libsystem_pthread.dylibpthread_kill + 90 frame #2: 0x0000000108fa019a libsystem_sim_c.dylib
abort + 129 frame #3: 0x0000000108d8b481 libc++abi.dylibabort_message + 257 frame #4: 0x0000000108db33d5 libc++abi.dylib
default_terminate_handler() + 267 frame #5: 0x0000000101e4be19 libobjc.A.dylib_objc_terminate() + 103 frame #6: 0x0000000108db0b01 libc++abi.dylib
std::__terminate(void (*)()) + 8 frame #7: 0x0000000108db07aa libc++abi.dylib__cxa_rethrow + 99 frame #8: 0x0000000101e4bd2c libobjc.A.dylib
objc_exception_rethrow + 40 frame #9: 0x00000001020db41e CoreFoundationCFRunLoopRunSpecific + 654 frame #10: 0x0000000104e74a3e GraphicsServices
GSEventRunModal + 161 frame #11: 0x00000001025ab8c0 UIKit`UIApplicationMain + 1282
- frame #12: 0x000000010176311f UTK Recruiting
main(argc=1, argv=0x00007fff5e4a04f8) + 111 at main.m:14 frame #13: 0x0000000108ef8145 libdyld.dylib
start + 1 frame #14: 0x0000000108ef8145 libdyld.dylib`start + 1thread #2: tid = 0x564a0, 0x0000000109201232 libsystem_kernel.dylib
kevent64 + 10, queue = 'com.apple.libdispatch-manager' frame #0: 0x0000000109201232 libsystem_kernel.dylib
kevent64 + 10 frame #1: 0x0000000108eb376c libdispatch.dylib_dispatch_mgr_invoke + 247 frame #2: 0x0000000108eb3511 libdispatch.dylib
_dispatch_mgr_thread + 54thread #3: tid = 0x564a2, 0x000000010920094a libsystem_kernel.dylib
__workq_kernreturn + 10 frame #0: 0x000000010920094a libsystem_kernel.dylib
__workq_kernreturn + 10 frame #1: 0x00000001092316c3 libsystem_pthread.dylib_pthread_wqthread + 869 frame #2: 0x000000010922f40d libsystem_pthread.dylib
start_wqthread + 13thread #4: tid = 0x564a3, 0x000000010920094a libsystem_kernel.dylib
__workq_kernreturn + 10 frame #0: 0x000000010920094a libsystem_kernel.dylib
__workq_kernreturn + 10 frame #1: 0x00000001092316c3 libsystem_pthread.dylib_pthread_wqthread + 869 frame #2: 0x000000010922f40d libsystem_pthread.dylib
start_wqthread + 13thread #5: tid = 0x564a4, 0x000000010920094a libsystem_kernel.dylib
__workq_kernreturn + 10 frame #0: 0x000000010920094a libsystem_kernel.dylib
__workq_kernreturn + 10 frame #1: 0x00000001092316c3 libsystem_pthread.dylib_pthread_wqthread + 869 frame #2: 0x000000010922f40d libsystem_pthread.dylib
start_wqthread + 13thread #6: tid = 0x564a5, 0x000000010920094a libsystem_kernel.dylib
__workq_kernreturn + 10 frame #0: 0x000000010920094a libsystem_kernel.dylib
__workq_kernreturn + 10 frame #1: 0x00000001092316c3 libsystem_pthread.dylib_pthread_wqthread + 869 frame #2: 0x000000010922f40d libsystem_pthread.dylib
start_wqthread + 13