UICollectionView scrollToItemAtIndexPath crash ios 10 - crash

I got this error on iOS 10, but it works fine both on iOS 11 and iOS 9.
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]'
Here is my code:
let indexPath = IndexPath(row: index, section: 0)
print(indexPath.row) // 5
//here is the problem. I got 6 items in my datasource but looks like collectionView still consider it's only 5.
print(collectionView.numberOfItems(inSection: 0)) // 6
if indexPath.row < collectionView.numberOfItems(inSection: 0) {
collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}
I've checked the numberOfItems before I call scrollToItem, but still got crash. Kind of weird, has anyone encountered this problem before?

It also happened to me. It seems that Apple introduced some modifications on iOS10 that forces to manually invalidateLayout() after reloading the data. This wasn't needed on iOS9 and it also not needed on iOS11. So, either they changed their mind or it was a bug :)
Just call invalidateLayout() to avoid the crash, like this:
self.collectionView.reloadData()
self.collectionView.collectionViewLayout.invalidateLayout() // Fix iOS10 crash

Related

Crash when setting badge on tab bar in objective C

This line of code crashes. How to check if tabor item is not nil then setting badge?
[self.tabController.tabBar.items objectAtIndex:0].badgeValue = [NSString stringWithFormat:#"%i", num];
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_CTNativeGlyphStorage
tabBar]: unrecognized selector sent to instance 0x15029cfd0'
I checked it by this line of code, but it didn't solve the problem.
if (self.tabController.tabBar.items != nil) { }
If you are setting the badge value before the tab controller is even presented or fully initiated, then there wont be anything at index 0 as yet. There is no need for the tab controller to initiate everything until time comes for presentation, for memory usage purposes. You can keep a reference to each instance of UITabBarItem and set the badge value on them. Or you are going to have to wait until viewDidAppear is called, ensuring all UITabBarItems are fully instantiated...
I cant assume anything else here without seeing the rest of your code.

NSUserDefaults crashes when adding empty array

I just moved to iOS 8, installed Xcode 6 etc.
This code worked nicely on iOS 7 and 6. It even seems to work on an iPhone 4S with iOS 8 installed. But on iPhones 5 and 5S it crashes. (8.0.0 and 8.0.2 alike)
It does not matter whether the app is build against SDK 7 or 8. It crashes in both cases at the same stage.
The .h file contains:
#property (nonatomic, strong) NSMutableArray* filterBrandsExclude;
from the .m file:
// Following lines just to set the conditions for your understanding:
_filterBrandsExclude = [[NSMutableArray alloc] initWithCapacity:0]; // just creates an empty array.
// As a matter of fact, this array may well be empty and it worked nicely on older iOS versions.
// this line crashes:
[userDefaults setObject:_filterBrandsExclude forKey:#"filterBrandsExclude"];
Error Message:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSKeyValueSlowMutableArray getObjects:range:]: value for key filterBrandsExclude of object 0x17004ae00 is nil'
*** First throw call stack:
(0x1885c6084 0x198bac0e4 0x1885c5fc4 0x18946097c 0x189460a68 0x189460ab0 0x1884c24d0 0x18854be90 0x1884c21c0 0x1884c17e8 0x188627c80 0x188552d9c 0x188551e9c 0x188600290 0x1885ff840 0x1886030e4 0x1893bb750 0x100053e5c 0x1000522a4 0x18cfd65d4 0x18cfdead0 0x18cfdea58 0x18cfd238c 0x1907d1640 0x18857e360 0x18857d468 0x18857ba8c 0x1884a9664 0x1915eb5a4 0x18cdae984 0x1000515f4 0x19921aa08)
libc++abi.dylib: terminating with uncaught exception of type NSException
Any idea is appreciated.
you can manually check
if(self.filterBrandsExclude != nil && self.filterBrandsExclude.count > 0)
[userDefaults setObject:_filterBrandsExclude forKey:#"filterBrandsExclude"];
else
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"filterBrandsExclude"];
Two things.
In your error it says that the value for the key filterBrandsInclude is nil, but in your question you reference the key filterBrandsExclude. Are you sure you've pinpointed the actual error at this point?
EDIT: Point #2 is invalid.

In App Purchase Crashes App

I have an iOS App that I just submitted an update for. The development version of the app (simulator and my 4S) never crashed and it works just fine. The app got approved today as well as the IAP, but I realized this morning that the iAP was not "cleared for purchase". I just cleared it for purchase, but the app still crashes. The error the iPhone gives (running the app store version) gives the error:
<Error>: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
Here is the code where that happens:
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:
(SKProductsResponse *)response
{
NSArray *myProduct = response.products;
//this line is where the error occurs
noAdProduct = [myProduct objectAtIndex:0];
if (![[[NSUserDefaults standardUserDefaults] objectForKey:#"the id here"] boolValue]) {
adCell.detailTextLabel.text = [NSString stringWithFormat:#"$%#", noAdProduct.price];
adCell.userInteractionEnabled = YES;
adCell.accessoryType = UITableViewCellAccessoryNone;
adCell.accessoryView = nil;
[self.tableView reloadData];
}
}
This happens while requesting a list of available products, which obviously returns the product in the simulator but not in the app. I just added a safeguard against this but I will need to submit it to the App Store again.
Does anyone know why the iAP is not showing up for the App Store version? Do I need to wait for the "cleared for sale" option to go to Apple's servers? Thanks!
Obviously myProduct array is empty. Why this happens is another question, but you should never call objectAtIndex without making sure the requested index exists.
if ([myProduct count] > 0)
noAdProduct = [myProduct objectAtIndex:0];
else
// Deal with the situation when the array is empty.

*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array. When pushing to a navigation controller

I am writing an iPad app. I load a view in with "initWithNibName" and then trying to push the view onto the Navigation controller.
[self.navigationController pushViewController:tvc animated:YES];
This causes the app to crash giving the following stackTrace
2011-09-01 12:46:06.040 PuckDragDemo[4932:f803] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
* First throw call stack:
(0x1191062 0x1322d0a 0x117ddc8 0x2979 0xae95 0x449ebe 0x44a1b1 0x45bccd 0x45bebf 0x45c18b 0x45cc2f 0x45d1d6 0x45cdf4 0x13975 0x1192ed9 0x386eb2 0x386e4a 0x42c3e6 0x42c8af 0x42bb6e 0x3ac2a0 0x3ac4c6 0x392c74 0x386399 0x1d87fa9 0x11651d5 0x10ca042 0x10c892a 0x10c7dd4 0x10c7ceb 0x1d86879 0x1d8693e 0x38438b 0x1c49 0x1bc5)
terminate called throwing an exception(gdb) bt
I have no idea why I am getting this error when I don't have an array. my only guess is that there is an array in the background somewhere which is getting called?
P.S the code requested
if (self.started == YES) return;
if (self.bEditMode == YES) {
[self go];
} else {
GetReadyModalViewController *modalVC = [[GetReadyModalViewController alloc] initWithNibName:#"GetReadyModelViewController" bundle:nil];
modalVC.delegate = self;
[self presentModalViewController:modalVC animated:NO];
modalVC.taskTitle.text = [self.taskPreferences objectForKey:#"title"];
modalVC.taskDescription.text = [self.taskPreferences objectForKey:#"description"];
}
}
It is irrelevant to the pushing on the navigationController, it is a problem inside of the tic viewController. Please post the code from the viewDidLoad/viewWillAppear, as your are instantiating an array and trying to get data from it [myMutableArray objectAtIndex:**0**]
Fixed it. I don't know exactly how it worked. There is a array in my code but not directly connected to the navigation controller. It appears that a index in the array was being called that didn't exist but for some reason it wasn't calling an error. Don't ask me why but when the navigation controller pushed the view it picked up on a previous undetected error.

" NSRangeException " problem

I am trying to build an iPhone app. But at the time of compiling there is a message showing that
[Session started at 2009-01-29 18:25:40 +0600.]
2009-01-29 18:25:44.238 SimpleGame[3691:20b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (1) beyond bounds (0)'
2009-01-29 18:25:44.247 SimpleGame[3691:20b] Stack: ( .........
Actually when I do active these code then this message is shown, code are:
NSMutableArray *todoArray = [[NSMutableArray alloc] init];
self.todos = todoArray;
[todoArray release];
How can I overcome this problem?
It seems like you're trying to access outside the bounds of an empty array.
put breakpoints on objc_exception_throw and [NSException raise]. This should show the error before it's actually thrown and should hopefully tell you enough to find the problem.
Tony