I have an NSMutableArray that I have build up that consists or holds a NSMutableDictionary.
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
self.userNameArray = [NSMutableArray arrayWithArray:[userDefaults arrayForKey:#"userNameTextArray"]];
NSMutableArray *arrayToAdd = [[NSMutableArray alloc] init];
for (id object in self.userNameArray) {
[arrayToAdd addObject:#"Negative"];
}
self.namesDictionary = [#{ #"userNameText" : self.categoriesMutableNameArray, #"selectedCellState" : arrayToAdd}mutableCopy];
self.namesFinalArr = [[NSMutableArray alloc] init];
self.namesDictMutableDict = [NSMutableDictionary dictionaryWithDictionary:self.namesDictionary];
[self.namesFinalArr addObject:self.namesDictMutableDict];
The result in my NSlog of the above code is like this:
(
{
selectedCellState = (
Negative,
Negative,
Negative,
Negative,
Negative,
Negative,
Negative,
Negative,
Negative,
Negative,
Negative,
Negative,
Negative,
Negative
);
userNameText = (
"userText - Text",
"userText1 - Text1",
"userText2 - Text2",
"userText3 - Text3",
"userText4 - Text4",
"userText5 - Text5",
"userText6 - Text6",
"userText7 - Text7",
"userText8 - Text8",
"userText9 - Text9",
"userText10 - Text10",
"userText11 - Text11",
"userText12 - Text12",
"userText13 - Text13"
);
}
)
I am using a UITableview and I populate the UITableview with self.namesFinalArr . in the CellForRow method like this and it works:
labelForTableCell.text = [[[self.namesFinalArr objectAtIndex:0] objectForKey:#"userNameText"]objectAtIndex:indexPath.row];
This populates my UITableview with the data under userNameText in self.namesFinalArr
I am trying to enable or disable an image on a cell when it is selected and I use the didDeselectRowAtIndexPath and didselectRowAtIndexPath methods to show and hide a UImageview
This works but I am trying to update selectedCellState in self.namesFinalArr at the index row or row that was pressed but I get the following error.
In the didselectRowAtIndexPath method I do something like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.selectedRows = [self.tableView indexPathsForSelectedRows];
[[[[self.namesFinalArr objectAtIndex:0] objectForKey:#"selectedCellState"]objectAtIndex:indexPath.row] setValue:#"Positive" forKey:#"selectedCellState"];
}
When trying to change the array value and index row I get a error:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFConstantString 0x23fef0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key selectedCellState.'
*** First throw call stack:
(
0 CoreFoundation 0x020c75e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01e4a8b6 objc_exception_throw + 44
2 CoreFoundation 0x021576a1 -[NSException raise] + 17
3 Foundation 0x0075d9ee -[NSObject(NSKeyValueCoding) setValue:forUndefinedKey:] + 282
4 Foundation 0x006c9cfb _NSSetUsingKeyValueSetter + 88
5 Foundation 0x006c9253 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 267
6 Piccing 0x0004880c -[PiccImageCategoriesViewController tableView:didSelectRowAtIndexPath:] + 828
7 UIKit 0x010a77b1 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1513
8 UIKit 0x010a7924 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 279
9 UIKit 0x010ab908 __38-[UITableView touchesEnded:withEvent:]_block_invoke + 43
10 UIKit 0x00fe2183 ___afterCACommitHandler_block_invoke + 15
11 UIKit 0x00fe212e _applyBlockToCFArrayCopiedToStack + 403
12 UIKit 0x00fe1f5a _afterCACommitHandler + 532
13 CoreFoundation 0x0208f4ce __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
14 CoreFoundation 0x0208f41f __CFRunLoopDoObservers + 399
15 CoreFoundation 0x0206d344 __CFRunLoopRun + 1076
16 CoreFoundation 0x0206cac3 CFRunLoopRunSpecific + 467
17 CoreFoundation 0x0206c8db CFRunLoopRunInMode + 123
18 GraphicsServices 0x027779e2 GSEventRunModal + 192
19 GraphicsServices 0x02777809 GSEventRun + 104
20 UIKit 0x00fc5d3b UIApplicationMain + 1225
21 Piccing 0x000138cd main + 141
22 libdyld.dylib 0x02f4d70d start + 1
23 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
In this case,there is one array which contains two dictionary each contains array and you are suppose to change the values in array and you should used this method to change the value
[[[self.namesFinalArr objectAtIndex:0] objectForKey:#"selectedCellState"] replaceObjectAtIndex:indexPath.row withObject:#"Positive"];
Related
I have Maintableview(a UIViewController) with contents and detailview(a UIViewController) that show detail for each of cells in Maintableview.
I have added a "favorite" button in "detailview" that user can add to Favoritetableview(a UIViewController) and every thing work fine.
Now I have added "add to favorite" in Maintableview with swipe to left.
It adds contents successfully to Favoritetableview but when I touch that cell in Favoritetableview the app crashes.
Below is the console logs for the crash:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(
0 CoreFoundation 0x000000010a871d85 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010a2e3deb objc_exception_throw + 48
2 CoreFoundation 0x000000010a75a934 -[__NSArrayI objectAtIndex:] + 164
3 iranbirds 0x00000001075cf672 -[FavoriteTableViewController prepareForSegue:sender:] + 530
4 UIKit 0x00000001090c55d5 -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 369
5 UIKit 0x00000001090c5433 -[UIStoryboardSegueTemplate _perform:] + 82
6 UIKit 0x0000000108b1b5f8 -[UIViewController performSegueWithIdentifier:sender:] + 99
7 iranbirds 0x00000001075cf42d -[FavoriteTableViewController tableView:didSelectRowAtIndexPath:] + 189
8 UIKit 0x0000000108ac51c6 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1887
9 UIKit 0x0000000108ac541b -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 388
10 UIKit 0x0000000108989f62 _runAfterCACommitDeferredBlocks + 317
11 UIKit 0x000000010899de4c _cleanUpAfterCAFlushAndRunDeferredBlocks + 95
12 UIKit 0x00000001089aa147 _afterCACommitHandler + 90
13 CoreFoundation 0x000000010a796c37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
14 CoreFoundation 0x000000010a796ba7 __CFRunLoopDoObservers + 391
15 CoreFoundation 0x000000010a78c7fb __CFRunLoopRun + 1147
16 CoreFoundation 0x000000010a78c0f8 CFRunLoopRunSpecific + 488
17 GraphicsServices 0x000000010be6bad2 GSEventRunModal + 161
18 UIKit 0x000000010897df09 UIApplicationMain + 171
19 iranbirds 0x00000001075d32ef main + 111
20 libdyld.dylib 0x000000010ba6492d start + 1
21 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
favorite table view:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = (NSIndexPath *)sender;
Favorite *fav = (Favorite *)[self.fetchedResultsController objectAtIndexPath:indexPath];
NSString *combinedName = fav.name;
if ([segue.identifier isEqualToString:#"FavoriteBirdDetail"])
{
GeneralViewController *detailViewController = (GeneralViewController*)[segue destinationViewController];
detailViewController.birdName = [combinedName componentsSeparatedByString:#"^"][0];;
detailViewController.sciName = [combinedName componentsSeparatedByString:#"^"][1];;
detailViewController.managedOjbectContext = self.managedOjbectContext;
}
}
This line cause error:
detailViewController.sciName = [combinedName componentsSeparatedByString:#"^"][1];
Any help would be appreciated.
detailViewController.sciName = [combinedName componentsSeparatedByString:#"^"][1];
Well, look at this code - you are getting second object from an array by subscript syntax. And the code would work if an array contains at least 2 items.
It looks like your array contains zero or one element, that is why app crashes when you try to get an element by 1 index - [1], which means the second element.
To prevent crash you should check an array count property before:
NSArray *anArray = [combinedName componentsSeparatedByString:#"^"];
if anArray.count >= 2 {
detailViewController.sciName = anArray[1];
}
I have a custom class oject that I want to save using NSUserDefaults, this is how I saving it:
Shift.m
-(void)encodeWithCoder:(NSCoder*)encoder
{
[encoder encodeObject:self.date forKey:#"date"];
[encoder encodeObject:self.startTime forKey:#"startTime"];
[encoder encodeObject:self.endTime forKey:#"endTime"];
}
- (id)initWithCoder:(NSCoder *)decoder {
if(self = [super init]) {
self.date = [decoder decodeObjectForKey:#"date"];
self.startTime = [decoder decodeObjectForKey:#"startTime"];
self.endTime = [decoder decodeObjectForKey:#"endTime"];
}
return self;
}
MyTableViewController.m:
-(void)saveCustomObject:(id)object forKey:(NSString*)key
{
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:object];
[[NSUserDefaults standardUserDefaults] setObject:encodedObject forKey:key];
}
-(NSArray*)getCustomObjectForKey:(NSString*)key
{
NSData *encodedObject = [[NSUserDefaults standardUserDefaults] objectForKey:key];
NSArray *shifts=[NSArray arrayWithObjects:[NSKeyedUnarchiver unarchiveObjectWithData:encodedObject], nil];
return shifts;
}
It seems it works fine when debugging, but when I try to access one of the object properties, for example on tableView: cellForRowAtIndexPath: like that:
Shift *currentShift=[self.shifts objectForIndex:indexPath.row];
NSLog(#"%#",currentShift.startTime.description);
It crashes with that crash message:
2016-03-19 09:04:05.913 MyApp[9654:4249448] -[__NSArrayM startTime]: unrecognized selector sent to instance 0x7ff81b449c10
2016-03-19 09:04:05.924 MyApp[9654:4249448] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM startTime]: unrecognized selector sent to instance 0x7ff81b449c10'
*** First throw call stack:
(
0 CoreFoundation 0x000000010e3e9e65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010de62deb objc_exception_throw + 48
2 CoreFoundation 0x000000010e3f248d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010e33f90a ___forwarding___ + 970
4 CoreFoundation 0x000000010e33f4b8 _CF_forwarding_prep_0 + 120
5 Mehuyavut count 0x000000010d95a8e8 -[shiftsTableViewViewController tableView:cellForRowAtIndexPath:] + 600
6 UIKit 0x000000010e8efe43 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 766
7 UIKit 0x000000010e8eff7b -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
8 UIKit 0x000000010e8c4a39 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2996
9 UIKit 0x000000010e8f901c -[UITableView _performWithCachedTraitCollection:] + 92
10 UIKit 0x000000010e8dfedc -[UITableView layoutSubviews] + 224
11 UIKit 0x000000010e84d4a3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
12 QuartzCore 0x000000011231959a -[CALayer layoutSublayers] + 146
13 QuartzCore 0x000000011230de70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
14 QuartzCore 0x000000011230dcee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
15 QuartzCore 0x0000000112302475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
16 QuartzCore 0x000000011232fc0a _ZN2CA11Transaction6commitEv + 486
17 QuartzCore 0x000000011233037c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
18 CoreFoundation 0x000000010e315367 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
19 CoreFoundation 0x000000010e3152d7 __CFRunLoopDoObservers + 391
20 CoreFoundation 0x000000010e30af2b __CFRunLoopRun + 1147
21 CoreFoundation 0x000000010e30a828 CFRunLoopRunSpecific + 488
22 GraphicsServices 0x0000000111ba6ad2 GSEventRunModal + 161
23 UIKit 0x000000010e796610 UIApplicationMain + 171
24 Mehuyavut count 0x000000010d95b5af main + 111
25 libdyld.dylib 0x0000000110b2592d start + 1
26 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Any one knows why?
Thank you!
It seems like you want Shift *currentShift=[self.shifts objectForIndex:indexPath.row] to give you 1 Shift object, but what it really does is returning a NSArray. You basically call a startTime method in NSArray, and since it doesn't exist, your program fails.
It seems like NSLog(#"%#",currentShift.startTime.description) is the line doing that. Confirm by commenting out the NSLog. It shouldn't give you a crash.
If this hypothesis is fine, check if [currentShift isKindOfClass:[NSArray class]] is true.
If it is, a bug you have in this line:
NSArray *shifts=[NSArray arrayWithObjects:[NSKeyedUnarchiver unarchiveObjectWithData:encodedObject], nil];
You also don't really show how your - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section looks like. You must return proper number of entries there.
You're making self.shifts be an NSArray, but you use objectForKey, which is available for dictionaries. It's not available for arrays. I think you wanted objectAtIndex instead.
I'm working on an iOS app with collectionView in objective-c and I have an error.
Below code is for feeding each cell with data(image in this case).
I pick up an image name from two dimensional array in Model class, and display it.
I made up a new array to prevent from duplication of image display by changing value in the array from '0' to '1' so next time can avoid display '1' marked image.
I succeeded initial setting all arrays with NSInteger 0, with replaceObjectAtIndex:withObject: method in a 'ViewDidLaod' method, however, I have an error when I want to change the value into '1' in the array with replaceObjectAtIndex:withObject:, the same method I used in the 'ViewDidLaod' method.
I already searched google and stack but people who got the same problem used immutable array, that's why they had an issue, but I have mutable array in my code. Please help me.
The error message is below:
2015-04-13 03:55:25.824 InfanTree[4474:166010] -[__NSCFNumber length]: unrecognized selector sent to instance 0xb000000000000002
2015-04-13 03:55:25.846 InfanTree[4474:166010] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0xb000000000000002'
*** First throw call stack:
(
0 CoreFoundation 0x000000010826fa75 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000107f08bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000108276d1d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00000001081ce9dc ___forwarding___ + 988
4 CoreFoundation 0x00000001081ce578 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000108f13f06 +[_UIAssetManager createAssetNamed:fromBundle:] + 67
6 UIKit 0x00000001088d4f60 +[UIImage imageNamed:inBundle:compatibleWithTraitCollection:] + 221
7 InfanTree 0x00000001079c92d0 -[ViewController collectionView:cellForItemAtIndexPath:] + 512
8 UIKit 0x0000000108ed1fab -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 244
9 UIKit 0x0000000108ed36e4 -[UICollectionView _updateVisibleCellsNow:] + 3445
10 UIKit 0x0000000108ed7391 -[UICollectionView layoutSubviews] + 243
11 UIKit 0x000000010891c1c3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
12 QuartzCore 0x000000010cc5ac58 -[CALayer layoutSublayers] + 150
13 QuartzCore 0x000000010cc4f87e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
14 QuartzCore 0x000000010cc4f6ee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
15 QuartzCore 0x000000010cbbd36e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
16 QuartzCore 0x000000010cbbe482 _ZN2CA11Transaction6commitEv + 390
17 UIKit 0x00000001088a068d -[UIApplication _reportMainSceneUpdateFinished:] + 44
18 UIKit 0x00000001088a1375 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2684
19 UIKit 0x000000010889fd35 -[UIApplication workspaceDidEndTransaction:] + 179
20 FrontBoardServices 0x000000010bd92243 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
21 CoreFoundation 0x00000001081a4c7c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
22 CoreFoundation 0x000000010819a9c5 __CFRunLoopDoBlocks + 341
23 CoreFoundation 0x000000010819a183 __CFRunLoopRun + 851
24 CoreFoundation 0x0000000108199bc6 CFRunLoopRunSpecific + 470
25 UIKit 0x000000010889f7a2 -[UIApplication _run] + 413
26 UIKit 0x00000001088a2580 UIApplicationMain + 1282
27 InfanTree 0x00000001079d19d3 main + 115
28 libdyld.dylib 0x000000010aa60145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Whole code is here
Below is part of my code.
#interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
{
DataModel *_data;
// save the indexPath value itself of the picture which is selected at specific index path
NSMutableArray *_displayedImageIndexPathHistory;
// save 1 for the first rendering so next time can avoid to be selected
NSMutableArray *_isSelectedBefore;
// save 1 if rendered at least once so can avoid duplication
NSMutableArray *_preventDuplication;
}
#end
#implementation ViewController
.
.
.
- (void)viewDidLoad {
[super viewDidLoad];
.
.
.
// _data.imageSet is a two dimensional 'MUTABLE' array which has 'MUTABLE' arrays in it.
// Declared in a Model class. Each element has NSString *imageName.png.
_preventDuplication = [_data.imageSet mutableCopy];
_displayedImageIndexPathHistory = [_data.imageSet mutableCopy];
_isSelectedBefore = [_data.imageSet mutableCopy];
// set all arrays with 0
NSInteger sectionNum = [_data.imageSet count];
NSNumber *defaultNumber = [NSNumber numberWithInt:0];
for (NSInteger i = 0; i < sectionNum; i++) {
for (NSInteger j = 0; j < [_data.imageSet[i] count]; j++) {
[_displayedImageIndexPathHistory[i] replaceObjectAtIndex:j withObject:defaultNumber];
[_isSelectedBefore[i] replaceObjectAtIndex:j withObject:defaultNumber];
[_preventDuplication[i] replaceObjectAtIndex:j withObject:defaultNumber];
}
}
.
.
.
}
.
.
.
-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
NSNumber *defaultNumber = [NSNumber numberWithInt:0];
NSNumber *markingNumber = [NSNumber numberWithInt:1];
if (indexPath.row == 0) {
cell.cellImageView.image = [UIImage imageNamed:[_data.imageSet[indexPath.section] objectAtIndex:indexPath.row]];
// **Have error at this sentence!!**
[_preventDuplication[indexPath.section] replaceObjectAtIndex:indexPath.row withObject: markingNumber];
}
else {
.
.
.
}
.
.
return cell;
}
Thanks in advance.
Your code has the expression:
[UIImage imageNamed:[_data.imageSet[indexPath.section] objectAtIndex:indexPath.row]]
It would seem that this part:
[_data.imageSet[indexPath.section] objectAtIndex:indexPath.row]
Is producing an instance of NSNumber, not an instance of NSString. Of course, +[UIImage imageNamed:] requires its argument to be a string. It's apparently invoking -length on it. Since NSNumber doesn't respond to -length, you get that exception and crash.
I got this error message.
GoogleAnalytics 2.0b3 void GAIUncaughtExceptionHandler(NSException *)
(GAIUncaughtExceptionHandler.m:41): Uncaught exception: *** -[__NSArrayM objectAtIndex:]:
index 2 beyond bounds [0 .. 1]
libc++abi.dylib: terminate_handler unexpectedly threw an exception
When I trying to zoom page, I got crash and this error in crashlytics.
Thread : Fatal Exception: NSRangeException
0 CoreFoundation 0x31a202a3 __exceptionPreprocess + 162
1 libobjc.A.dylib 0x3974897f objc_exception_throw + 30
2 CoreFoundation 0x3196bb75 -[__NSArrayM objectAtIndex:] + 164
3 TestApp 0x000b1bfb -[EpisodeTableViewDelegate
tableView:didSelectRowAtIndexPath:] (EpisodeTableViewDelegate.m:155)
4 UIKit 0x338ea28d -[UITableView
_selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 876
5 UIKit 0x3396cf81 -[UITableView
_userSelectRowAtPendingSelectionIndexPath:] + 156
6 Foundation 0x3232e277 __NSFireDelayedPerform + 450
7 CoreFoundation 0x319f55df __
CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 14
8 CoreFoundation 0x319f5291 __CFRunLoopDoTimer + 272
9 CoreFoundation 0x319f3f01 __CFRunLoopRun + 1232
10 CoreFoundation 0x31966ebd CFRunLoopRunSpecific + 356
11 CoreFoundation 0x31966d49 CFRunLoopRunInMode + 104
12 GraphicsServices 0x3553f2eb GSEventRunModal + 74
13 UIKit 0x3387c301 UIApplicationMain + 1120
14 Tapastic 0x00075cb7 main (main.m:16)
15 Tapastic 0x000753b8 start
My code is as below:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
if (indexPath.section == 1) {
Episode *episode = [[Episode alloc] init];
[episode bindProperties:[self.pager.contents objectAtIndex:indexPath.row]];
[self.baseController performSegueWithIdentifier:SEGUE_EPISODE_VIEW sender:episode];
}
}
I cannot solve this problem. How can I fix it? If possible, please let know.
Thanks in advance.
You're calling objectAtIndex on the episode array with index 2, when there are only two objects in the array. Array objects are numbered starting with 0, so an array with two objects only has elements 0 and 1.
It sounds like you're not setting up your UITableView correctly. If there are only two objects in the array backing the table, there should only be two rows in the table.
I get this exception in my Crashalytics:
0 CoreFoundation
__exceptionPreprocess + 162
1 libobjc.A.dylib
objc_exception_throw + 30
2 CoreFoundation
-[NSException initWithCoder:]
3 CoreFoundation
-[__NSCFArray objectAtIndex:] + 136
4 BusinessPlan ✭ BusinessController.m line 167
-[BusinessController tableView:didSelectRowAtIndexPath:] + 167
5 UIKit
-[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 876
6 UIKit
-[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 156
7 Foundation
__NSFireDelayedPerform + 450
8 CoreFoundation
__CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 14
9 CoreFoundation
__CFRunLoopDoTimer + 272
10 CoreFoundation
__CFRunLoopRun + 1232
11 CoreFoundation
CFRunLoopRunSpecific + 356
12 CoreFoundation
CFRunLoopRunInMode + 104
13 GraphicsServices
GSEventRunModal + 74
14 UIKit
UIApplicationMain + 1120
15 BusinessPlan main.m line 16
main + 16
16 BusinessPlan
start
and it is called this:
Exception Type: NSRangeException
Reason: -[__NSCFArray objectAtIndex:]: index (14) beyond bounds (14)
It only happened once so I am not sure how to reproduce it. But it happened in this code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *info = [topics_array objectAtIndex:indexPath.row];
NSString *section_name = [info objectForKey:#"section_name"];
NSString *solution_section_id = [info objectForKey:#"solution_section_id"];
// PUT THESE ITEMS INTO THE SESSION
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setObject:section_name forKey:#"section_name"];
[standardUserDefaults setObject:solution_section_id forKey:#"solution_section_id"];
[standardUserDefaults synchronize];
[self performSegueWithIdentifier:#"BusinessToTopic" sender:self];
}
Would anyone know how to prevent this from happening in the future and why it is happening?
EDIT:
This is the code for tableView numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableLabels count];
}
Thanks!