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];
}
Related
I am getting an error
2014-04-09 01:42:23.599 appname[9584:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<LoginViewController: 0x9a9cda0>) has no segue with identifier 'login''
*** First throw call stack:
(
0 CoreFoundation 0x0263c1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x023bb8e5 objc_exception_throw + 44
2 UIKit 0x0119c48c -[UIViewController shouldPerformSegueWithIdentifier:sender:] + 0
3 appname 0x00002fea -[LoginViewController viewDidAppear:] + 170
4 UIKit 0x0119e099 -[UIViewController _setViewAppearState:isAnimating:] + 526
5 UIKit 0x0119e617 -[UIViewController __viewDidAppear:] + 146
6 UIKit 0x011a0014 __64-[UIViewController viewDidMoveToWindow:shouldAppearOrDisappear:]_block_invoke + 44
7 UIKit 0x0119e9aa -[UIViewController _executeAfterAppearanceBlock] + 63
8 UIKit 0x010990d0 ___afterCACommitHandler_block_invoke_2 + 33
9 UIKit 0x01099055 _applyBlockToCFArrayCopiedToStack + 403
10 UIKit 0x01098e9a _afterCACommitHandler + 568
11 CoreFoundation 0x0260436e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
12 CoreFoundation 0x026042bf __CFRunLoopDoObservers + 399
13 CoreFoundation 0x025e2254 __CFRunLoopRun + 1076
14 CoreFoundation 0x025e19d3 CFRunLoopRunSpecific + 467
15 CoreFoundation 0x025e17eb CFRunLoopRunInMode + 123
16 GraphicsServices 0x042205ee GSEventRunModal + 192
17 GraphicsServices 0x0422042b GSEventRun + 104
18 UIKit 0x0107bf9b UIApplicationMain + 1225
19 appname 0x00004e3d main + 141
20 libdyld.dylib 0x02ed9701 start + 1
21 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
However I do not think I am doing something wrong with my code this is my viewdiappear. What did I miss out so that this kind of error is created?
- (void)viewDidAppear:(BOOL)animated {
PFUser *user = [PFUser currentUser];
if (user.username != nil) {
[self performSegueWithIdentifier:#"login" sender:self];
}
}
It seems like you haven't set the name of the segue in the storyboard. The picture below is the name of one of my segues, for you, the text box would contain simply login.
It can also happen when you forget to "Embed" the storyboard in Navigation controller:
What the error is telling you is that although your storyboard may contain a segue called #"login", it does not emanate from this LoginViewController (the one that is currently self).
You may think it does, but trust me, the runtime knows more than you do.
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"];
I am trying to get the contents of my UITableViewCell before I delete it from my Core Data model in order to act on its contents. If there is only one item in the Core Data model and I go to delete it from my UITableView, the app will only sometimes throw an error of 2014-01-05 11:10:26.189 Nibbles[43609:70b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFArray objectAtIndex:]: index (2008) beyond bounds (1)'
My biggest confusion is that this only happens with one item in the UITableView and doesn't happen 100% of the time. If you need to see any other code, please let me know.
Here is the code I am using. The bolded line is the one causing the error since it never makes it past there to NSLog to the console.
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(#"Delete button pressed!");
FoodListCell *cell = (FoodListCell*)[tableView cellForRowAtIndexPath:indexPath];
NSLog(#"DEBUG | Selected Cell: %#", cell);
NSString *foodOrActivity = cell.foodNameLabel.text;
NSString *points = cell.foodPointsLabel.text;
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
[[self.fetchedResultsController objectAtIndexPath:indexPath] MR_deleteInContext:localContext];
[localContext MR_saveOnlySelfAndWait];
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
NSManagedObject *deleteObject = [_fetchedResultsController objectAtIndexPath:path];
[managedObjectContext deleteObject:deleteObject];
}
And here is the full stack trace:
2014-01-05 11:10:26.189 Nibbles[43609:70b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFArray objectAtIndex:]: index (2008) beyond bounds (1)'
*** First throw call stack:
(
0 CoreFoundation 0x01cd45e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01a578b6 objc_exception_throw + 44
2 CoreFoundation 0x01cd43bb +[NSException raise:format:] + 139
3 CoreData 0x00280755 -[_PFArray objectAtIndex:] + 133
4 CoreData 0x002f9778 -[_PFMutableProxyArray objectAtIndex:] + 120
5 CoreData 0x00382c1f -[NSFetchedResultsController objectAtIndexPath:] + 255
6 Nibbles 0x0000660d -[FoodListViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 685
7 UIKit 0x008b5ba3 -[UITableView animateDeletionOfRowWithCell:] + 107
8 UIKit 0x00a35695 -[UITableViewCell _swipeDeleteButtonPushed] + 70
9 libobjc.A.dylib 0x01a69874 -[NSObject performSelector:withObject:withObject:] + 77
10 UIKit 0x007c70c2 -[UIApplication sendAction:to:from:forEvent:] + 108
11 UIKit 0x007c704e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
12 UIKit 0x008bf0c1 -[UIControl sendAction:to:forEvent:] + 66
13 UIKit 0x008bf484 -[UIControl _sendActionsForEvents:withEvent:] + 577
14 UIKit 0x008be733 -[UIControl touchesEnded:withEvent:] + 641
15 UIKit 0x00b39c7f _UIGestureRecognizerUpdate + 7166
16 UIKit 0x0080419a -[UIWindow _sendGesturesForEvent:] + 1291
17 UIKit 0x008050ba -[UIWindow sendEvent:] + 1030
18 UIKit 0x007d8e86 -[UIApplication sendEvent:] + 242
19 UIKit 0x007c318f _UIApplicationHandleEventQueue + 11421
20 CoreFoundation 0x01c5d83f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
21 CoreFoundation 0x01c5d1cb __CFRunLoopDoSources0 + 235
22 CoreFoundation 0x01c7a29e __CFRunLoopRun + 910
23 CoreFoundation 0x01c79ac3 CFRunLoopRunSpecific + 467
24 CoreFoundation 0x01c798db CFRunLoopRunInMode + 123
25 GraphicsServices 0x0285e9e2 GSEventRunModal + 192
26 GraphicsServices 0x0285e809 GSEventRun + 104
27 UIKit 0x007c5d3b UIApplicationMain + 1225
28 Nibbles 0x000236ad main + 141
29 libdyld.dylib 0x031b470d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
The conventional pattern is to access the model (not through a table cell, but directly from the model). Do whatever you need to do with it, then remove it from your model, then remove it from your table...
The error implies that the localContext being accessed this method is different from (or is in a different state than) the MOC that reports the number of items in the model (used in numberOfRowsInSection:)
Before moving on to recording that deleted object's state, or deleting it. Fix the code that gets that object and just NSLog it. The code that gets that object should exactly match the code that gets it in your cellForRowAtIndexPath:, and the MOC used there should exactly match the numberOfRowsInSection: MOC.
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
NSManagedObject *deleteObject = [_fetchedResultsController objectAtIndexPath:path];
[managedObjectContext deleteObject:deleteObject];
Here you are getting the index path for the row in section 0 and deleting it.
You already have the index path for the row/cell/section that you want to delete, so why are you trying to delete that row only in section 0?
Try a breakpoint at this line and add some logging to show what row in section 0 is trying to be deleted.
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've got a problem with my iOS app. I've built my app with storyboard and some code. Initially, i've had a TableView which display some content in the first tab. Now i would like to put this content in another tab, but when i edit my storyboard i get this error :
2013-11-28 17:31:17.354 devis_centrage[13961:70b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(
0 CoreFoundation 0x0173a5e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014bd8b6 objc_exception_throw + 44
2 CoreFoundation 0x016ee9c2 -[__NSArrayI objectAtIndex:] + 210
3 CoreFoundation 0x017b9608 -[NSArray objectAtIndexedSubscript:] + 40
4 devis_centrage 0x000022f8 -[AppDelegate application:didFinishLaunchingWithOptions:] + 936
5 UIKit 0x00225355 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 309
6 UIKit 0x00225b95 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1536
7 UIKit 0x0022a3a8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
8 UIKit 0x0023e87c -[UIApplication handleEvent:withNewEvent:] + 3447
9 UIKit 0x0023ede9 -[UIApplication sendEvent:] + 85
10 UIKit 0x0022c025 _UIApplicationHandleEvent + 736
11 GraphicsServices 0x036e12f6 _PurpleEventCallback + 776
12 GraphicsServices 0x036e0e01 PurpleEventCallback + 46
13 CoreFoundation 0x016b5d65 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
14 CoreFoundation 0x016b5a9b __CFRunLoopDoSource1 + 523
15 CoreFoundation 0x016e077c __CFRunLoopRun + 2156
16 CoreFoundation 0x016dfac3 CFRunLoopRunSpecific + 467
17 CoreFoundation 0x016df8db CFRunLoopRunInMode + 123
18 UIKit 0x00229add -[UIApplication _run] + 840
19 UIKit 0x0022bd3b UIApplicationMain + 1225
20 devis_centrage 0x00002ccd main + 141
21 libdyld.dylib 0x01d7870d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Here the code for the array initialization :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_aircraft = [NSMutableArray arrayWithCapacity:20];
Aircraft *aircraft = [[Aircraft alloc] init];
aircraft.name = #"Robin DR400";
aircraft.immat = #"F-HAZA";
[_aircraft addObject:aircraft];
aircraft = [[Aircraft alloc] init];
aircraft.name = #"Tecnam P2002 JF";
aircraft.immat= #"F-HAZB";
[_aircraft addObject:aircraft];
aircraft = [[Aircraft alloc] init];
aircraft.name = #"Tecnam P2002 JF";
aircraft.immat= #"F-HAZC";
[_aircraft addObject:aircraft];
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController = [tabBarController viewControllers][0];
AircraftViewController *aircraftViewController = [navigationController viewControllers][0];
aircraftViewController.aircraft = _aircraft;
return YES;
}
You are accessing second object in an array. If you try to access it then you it(object) to be in your array. Maybe you forgot to init it.