App crashing on iPad but not simulator - ios

My Application is crashing when I run it on the iPad but works 100% on the iPad simulator I am using Version 4.6.1 of Xcode and Version 6.1.3 on the iPad.
The problem lies where I am trying to pass the value of an int between segues
in my .h
#property (nonatomic, assign)int currentQuestion;
in my .m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"level1correct"]){
AddLevel1IncorrectViewController *incorrect = [segue destinationViewController];
incorrect.CQValue = self.currentQuestion;
}}
AddLevel1Incorrect.h
#property (nonatomic, assign)int CQValue;
AddLevel1Incorrect.m
#synthesize CQValue = _CQValue;
- (void)imageSelect{
int numItems = [arrayPath count];
NSMutableArray *left = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *right = [NSMutableArray arrayWithCapacity:numItems];
for (NSDictionary *itemData in arrayPath) {
[left addObject:[itemData objectForKey:#"L"]];
[right addObject:[itemData objectForKey:#"R"]];
}
NSLog(#" value of %d CQValue ", self.CQValue);
leftImageViewer.image = [UIImage imageNamed:left[self.CQValue]];//this is the point where the crash happens
rightImageViewer.image = [UIImage imageNamed:right[self.CQValue]];
}
The interesting thing is it does display the correct value in the NSLog in the Console as you will see at the top of the Crash Message
2013-04-03 22:50:00.404 thefyp[1506:907] value of 1 CQValue
2013-04-03 22:50:00.408 thefyp[1506:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array'
*** First throw call stack:
Any ideas where I am going wrong here?

Your code is very brittle, meaning it makes a lot of assumptions about the data it's working with without verifying the data is accurate.
You never check the bounds of the array before you access it. self.CQValue is 1, but in this case the array itself is empty. so left[self.CQValue], is left[1], which is invalid.
Check arrayPath to make sure it's not empty.

Related

Objective C program - throwing an exception

I was testing an app on Xcode and I got this error saying that an exception has been thrown. Using the debugger, I added an exception breakpoint which led me to this:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.delegate = self;
int index = [[NSUserDefaults standardUserDefaults] integerForKey:#"ChecklistIndex"];
if (index != -1) {
Checklist *checklist = [self.dataModel.lists objectAtIndex:index];
[self performSegueWithIdentifier:#"ShowChecklist" sender:checklist];
}
}
With this being the highlighted line:
Checklist *checklist = [self.dataModel.lists objectAtIndex:index];
And the debugger said this:
-[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array
I'm not too sure how to fix this. If any more information is needed let me know. Thanks.
It looks like your self.dataModel.lists property is empty.
You should look at where you actually set that ".lists" property or "dataModel" property. One of them is not what you think it is.

NSInvalidArgumentException in Objective C

I'm making a simple calculator and when I use the addition button, the iOS Simulator crashes and I get an NSInvalidArgumentException. What do I do to prevent this from happening?
Error Report:
2013-06-23 17:18:54.574 Tutorial Test (Storyboard)[9744:c07] -[ViewController2 addition]:
unrecognized selector sent to instance 0x75858f0
2013-06-23 17:18:54.577 Tutorial Test (Storyboard)[9744:c07] *** Terminating app due to
uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController2 addition]:
unrecognized selector sent to instance 0x75858f0'
*** First throw call stack:
(0x1c91012 0x10cee7e 0x1d1c4bd 0x1c80bbc 0x1c8094e 0x10e2705 0x162c0 0x16258 0xd7021
0xd757f 0xd66e8 0x45cef 0x45f02 0x23d4a 0x15698 0x1becdf9 0x1becad0 0x1c06bf5 0x1c06962
0x1c37bb6 0x1c36f44 0x1c36e1b 0x1beb7e3 0x1beb668 0x12ffc 0x1e2d 0x1d55)
libc++abi.dylib: terminate called throwing an exception
(lldb)
Code in ViewController that has this error:
View Controller.h
#import <UIKit/UIKit.h>
//Calculator
#interface ViewController2 : UIViewController{
IBOutlet UITextField *textField1;
IBOutlet UITextField *textField2;
IBOutlet UILabel *label;
}
-(IBAction)addition;
-(IBAction)subtract;
-(IBAction)multiply;
-(IBAction)divide;
-(IBAction)clear;
#end
View Controller.m
//Addition
-(IBAction)plus
{
float a = ([textField1.text floatValue]);
float b = a+([textField2.text floatValue]);
label.text = [[NSString alloc] initWithFormat:#"%2.f", b];
}
You named the method in the .m file plus, rather than addition. That is why the method addition isn't found, and the fix would be to rename the method in the .m file from plus to addition:
//Addition
-(IBAction)addition
{
float a = [textField1.text floatValue];
float b = [textField2.text floatValue];
label.text = [[NSString alloc] initWithFormat:#"%2.f", a+b];
}
Have you allocated an instance of the ViewController2 class?
For example:
ViewController2 *vc2 = [[ViewController2 alloc] init];
[vc2 addition];
Also, if the .m code you posted is the full contents of that file, then you're also missing
-(IBAction)addition {
...
}
You didn't inplement the right method.
You call addition but you never actually implement it. Declaring the method signature in the .h is not enough, you have to code the method.
This means that the selector that you are calling is not available on the class you called it against. The best thing to do is place a breakpoint where the method is being called. Go to the debugger and po your object.
po 0x75858f0
in this instance. However that number could be an object and it could be different. Look at the number in the console.
Make sure the object is of the proper class. Then check that class to make sure it responds to the selector you are calling.
Edit: Your problem is that your plus method should be renamed to addition. The names in the .h must match the names in the .m

When i click an item in my collection view i got this error

I have a collectionview but when i tried to click the image the it showing this error ? The whole application is a tabbar application ,so all other tabs are working . except the last tab images.
iam taking images from plist for displaying it into collectionview
this is error
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI
objectAtIndex:]: index 0 beyond bounds for empty array'*** First throw call stack: (0x1e92012
0x1969e7e 0x1e47b44 0x58b91 0x56686 0x137cf 0x98f1c7 0x98f232 0x98f4da 0x9a68e5 0x9a69cb 0x9a6c76
`0x9a6d71 0x9a789b 0x9a7e93 0x9a7a88 0xd03e63 0xcf5b99 0xcf5c14 0xdaf5d9 0xdc1182 0xdc1394 0x197d705
0x9bb93c 0x9bb9ac 0x197d705 0x9bb93c 0x9bb9ac 0xb751d3 0x1e5aafe 0x1e5aa3d 0x1e387c2 0x1e37f44
0x1e37e1b 0x3c0f7e3 0x3c0f668 0x8adffc 0x287d 0x27a5) libc++abi.dylib: terminate called throwing an exception
This is the didselect method and preparesegue for passing that array to another viewcontroller
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"bookItem"]) {
NSDictionary *currentBook = [[_freeBooks objectForKey:#"freebook"] objectAtIndex:[[[self.collectionView indexPathsForSelectedItems] objectAtIndex:0] row]];
NSLog(#"%#", currentBook);
GMMDetailpage *detailpage = (GMMDetailpage *)[segue destinationViewController];
detailpage.bookDetails = currentBook;
NSLog(#"%#",detailpage.bookDetails);
}
}
The value of current book is printing and its fine.... but after this it executes some other system codes and then it shows exception .check image
You are doing two array accesses, so the error comes from the fact that one of the two arrays is empty.
The two arrays are
[self.collectionView indexPathsForSelectedItems]
and
[_freeBooks objectForKey:#"freebook"]
Check both the array independently and find out which one is the empty one.

how to solve NSRangeException

hi i am new to iphone programing,i am using EGOPhotViewer and want to show images by using this code,
for ( recipeImages in recipeImages.imgArray) {
photo = [[MyPhoto alloc] initWithImageURL:[NSURL URLWithString:recipeImages.recipie_img_url]name:recipeImages.recipe_name];
NSLog(#"%#",recipeImages.recipie_img_url);
MyPhotoSource *source = [[MyPhotoSource alloc] initWithPhotos:[NSArray arrayWithObjects:photo ,nil]];
photoController = [[EGOPhotoViewController alloc] initWithPhotoSource:source];
}
[APPDELEGATE.navigationController pushViewController:photoController animated:YES];
and i get this error
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2147483648 beyond bounds [0 .. 0]'
*** First throw call stack:
(0x225f012 0x2084e7e 0x2214b44 0x9e1e4 0xa46c4 0x1b45dc9 0x22b90c5 0x2213efa 0x1a7a482 0x1a8d73b 0xa9d7c 0xa6a4e 0xa5081 0xa0499 0x10af753 0x10afa7b 0x10bd590 0x10c55bd 0x10c5eab 0x10c64a3 0x10c6098 0x5bad6 0x2098705 0xfcf920 0xfcf8b8 0x1090671 0x1090bcf 0x108fd38 0xfff33f 0xfff552 0xfdd3aa 0xfcecf8 0x2c15df9 0x2c15ad0 0x21d4bf5 0x21d4962 0x2205bb6 0x2204f44 0x2204e1b 0x2c147e3 0x2c14668 0xfcc65c 0x263a 0x2545)
libc++abi.dylib: terminate called throwing an exception
i solved this by writing this code
NSMutableArray *localImagesArray = [[NSMutableArray alloc] init];
for ( recipeImages in recipeImages.imgArray) {
photo = [[MyPhoto alloc] initWithImageURL:[NSURL URLWithString:recipeImages.recipie_img_url]name:recipeImages.recipe_name];
NSLog(#"%#",recipeImages.recipie_img_url);
NSLog(#"%#", [photo debugDescription]);
[localImagesArray addObject:photo];
}
MyPhotoSource *source = [[MyPhotoSource alloc] initWithPhotos:localImagesArray];
photoController = [[EGOPhotoViewController alloc] initWithPhotoSource:source];
[APPDELEGATE.navigationController pushViewController:photoController animated:YES];
}
[__NSArrayI objectAtIndex:]: index 2147483648 beyond bounds [0 .. 0]'
2147483648 is NSNotFound. Somewhere in your code, or the code of a library you are using, something like indexOfObject: is being used on one array, and that index is being used to get a value from another array, and it is failing.
Your for loop looks very suspect. You're assigning a value to photoController at the end of each iteration, meaning only the value you assign last will actually get used. I'm not familiar with the library you're using but you probably want to be building up an array of MyPhoto objects before passing them to a single photoController.
Make sure initWithImageURL: name: inside MyPhoto returns self.
Verify this with NSLog(#"%#", [photo debugDescription]);

Can't set NSArray in segue

DBPostListViewController has a NSArray *postList that needs to be set.
I keep getting
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewController setPostList:]: unrecognized selector sent to instance 0x6e493d0
within the prepareForSegue for the setPostList function here:
NSArray *tempAry = [str componentsSeparatedByString:#"|#|"];
NSMutableArray *postArray = [[NSMutableArray alloc] initWithCapacity:[tempAry count]];
for (NSString *entry in tempAry) {
DBPost *tempPost = [[DBPost alloc] initWithString:entry];
[postArray addObject:tempPost];
}
DBPostListViewController *dest = [segue destinationViewController];
[dest setPostList:postArray];
Your error message explains it:
-[UITableViewController setPostList:]: unrecognized selector
You send that message on these lines of code:
DBPostListViewController *dest = [segue destinationViewController];
[dest setPostList:postArray];
Objective-C is dynamically typed, so even though you've declared dest to be a DBPostListViewController, it could be any Objective-C object at all. And in this case it happens to be a UITableViewController.
You forgot to set the custom class in your storyboard file, so UIKit is just making an instance of a plain UITableViewController. Fix your storyboard and re-run your app.

Resources