Crash when setting badge on tab bar in objective C - 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.

Related

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.

program crashes when trying to present modal view controller

I want to load another xib, and use this simple code:
AddElementViewController *viewToLoad = [[AddElementViewController alloc] initWithNibName:#"AddElementViewController" bundle:nil];
viewToLoad.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:viewToLoad animated:YES];
for some reason it throws me out with this output:
2012-03-11 11:56:57.990 Weesh[14650:11603] -[MainViewController AddPressed:]: unrecognized selector sent to instance 0x7b55b40
2012-03-11 11:56:57.991 Weesh[14650:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainViewController AddPressed:]: unrecognized selector sent to instance 0x7b55b40'
*** First throw call stack:
(0x13da052 0x198ed0a 0x13dbced 0x1340f00 0x1340ce2 0x13dbec9 0x32a5c2 0x32a55a 0x3cfb76 0x3d003f 0x3cf2fe 0x34fa30 0x34fc56 0x336384 0x329aa9 0x22c7fa9 0x13ae1c5 0x1313022 0x131190a 0x1310db4 0x1310ccb 0x22c6879 0x22c693e 0x327a9b 0x1cad 0x1c15)
terminate called throwing an exception(lldb)
do you have any idea what can cause it?
Somewhere in your code (it seems to be in AddElementViewController implementation) you are sending message AddPressed: to object of class MainViewController.
The call stack and crash error says that there are no method AddPressed: in class MainViewController.
Try to find all places where you are calling AddPressed: (first of all check loadView, viewDidLoad, viewWillAppear, viewDidAppear, init of AddElementViewController class) and check the type of receiver.

*** -[__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.

Exception: unrecognized selector ... when trying to push a view controller on Navigation stack

I am trying to push a view controller on the navigation stack with following code in my buttonPressed method
-(IBAction) viewButtonPressed:(id)sender {
PersonDetailViewController *personDetailViewController = [[PersonDetailViewController alloc] initWithNibName:#"PersonDetailViewController" bundle:nil];
[self.navigationController pushViewController:personDetailViewController animated:YES];
[personDetailViewController release];
}
I ran the debugger and it throws the exception after followin line is stepped over from above code.
[self.navigationController pushViewController:personDetailViewController animated:YES];
This is pretty standard call ... it has worked many times before without any problems. Any idea what am I missing here?
I am stuck at this point and not able to drill down further.
Here are the error messages on the console after running the debugger
2009-10-30 18:15:11.127 Untitled[6089:20b] * -[NSCFString image]: unrecognized selector sent to instance 0x3050
2009-10-30 18:15:11.129 Untitled[6089:20b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString image]: unrecognized selector sent to instance 0x3050'
2009-10-30 18:15:11.130 Untitled[6089:20b] Stack: (
807902715,
2460638779 ...
The problem was in one of the init methods where I was setting the back button item incorrectly.
I would start by looking inside the view lifecycle methods - viewWillAppear, viewDidAppear etc. - in the PersonDetailViewController class for invocations of a method named image on some object instance.
You can also try stepping through these methods in an attempt to narrow down where this invocation is made.