Terminating app due to uncaught exception 'NSRangeException', reason - ios

I am using search bar in collection,when I am doing filter text I got the error like
` Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2147483647 beyond bounds [0 .. 37]'
*** First throw call stack:
`
Here is my code
- (void)filterListForSearchText:(NSString *)searchText
{
for (NSString *title in _arrayCCName) {
NSRange nameRange = [title rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (nameRange.location != NSNotFound) {
[_searchResultName addObject:title];
}
}
for (int i=0; i<_searchResultName.count; i++) {
NSString *str=[ObjCls objectAtIndex:i];
NSInteger index=[_searchResultName indexOfObject:str];
[_searchResultDeignation addObject:[_arrayCCDesignation objectAtIndex:index]];
[_searchResultProfilePicture addObject:[_arrayCCProfilePicture objectAtIndex:index]];
[_searchResultFamilyPicture addObject:[_arrayCCFamilyPicture objectAtIndex:index]];
NSLog(#"array index is %ld",(long)index);
}
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
SEARCHBAR.showsCancelButton=NO;
[self.view endEditing:YES];
}
I got above error please help me how it is solve?

Simply means that the value of index is greater than the size of array at some point. When using [array objecAtIndex:index], index must be less than array.count.

These lines here....
NSInteger index=[_searchResultName indexOfObject:str];
[_searchResultDeignation addObject:[_arrayCCDesignation objectAtIndex:index]];
[_searchResultProfilePicture addObject:[_arrayCCProfilePicture objectAtIndex:index]];
[_searchResultFamilyPicture addObject:[_arrayCCFamilyPicture objectAtIndex:index]];
NSLog(#"array index is %ld",(long)index);
You are assuming that the indexOfObject actually finds something. What happens if the str doesn't exist
Taken from the documentation....
Declaration
OBJECTIVE-C
- (NSUInteger)indexOfObject:(id)anObject
Parameters
anObject
An object.
Return Value
The lowest index whose corresponding array value is equal to anObject. If none of the objects in the array is equal to anObject, returns NSNotFound.

Related

UISearchBar unrecognized selector sent to instance

I've a .json file stored locally from which I'm loading in an array called countries as countries = (NSArray *)[NSJSONSerialization JSONObjectWithData [fileContents dataUsingEncoding:NSUTF8StringEncoding] options:0 error:NULL]; after loading the filepath ofcourse. I'm Loading the .json into countries array as:
NSString * filePath =[[NSBundle mainBundle] pathForResource:#"countries" ofType:#"json"];
NSError * error;
NSString* fileContents =[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if(error||!filePath)
{
NSLog(#"Error reading file: %#",error.localizedDescription);
}
countries = (NSArray *)[NSJSONSerialization
JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:NULL];
After that I've taken an NSMutableArraycalled displayItems and initialized it as displayItems = [[NSMutableArray alloc] initWithArray:countries];. Than in UITableView delegate methods I've used displayItems in count which after NSLog gives 248 means it's correctly getting the values cellForRowAtIndexPath and didSelectRowAtIndexPath See this image below for the hierarchy of the ViewController
THE PROBLEM:
Based on the break points, this method is creating issue.
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if ([searchText length] == 0) {
[displayItems removeAllObjects];
[displayItems addObjectsFromArray:countries];
} else {
[displayItems removeAllObjects];
for (NSString *string in countries) {
NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (r.location != NSNotFound) {
[displayItems addObject:string];
}
}
}
[tableView reloadData];
}
When it reaches the line Range r the app crashes and the error is unrecognized selector sent to instance. Everything is working great until I type a letter in the UISearchBar the app crashes at that time.
The Exact Error is:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x12ddd5e90'
*** First throw call stack:
(0x180a55900 0x1800c3f80 0x180a5c61c 0x180a595b8 0x18095d68c 0x1000d0e3c 0x185953d28 0x18577fe50 0x18577fdcc 0x185767a88 0x185953ad4 0x18578a8f8 0x18591fee8 0x18591f104 0x185aafae4 0x186095b20 0x18591ee70 0x185b5cc90 0x185b5c854 0x185772e84 0x181417e20 0x180a0cefc 0x180a0c990 0x180a0a690 0x180939680 0x181e48088 0x1857b0d90 0x1000e0e94 0x1804da8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

NSRange changes itself inside for loop

I have an NSDictionary of NSRange objects, with keys for their index in the array. I am attempting to use each of these ranges to create substrings of a larger string, and place each of these substrings in the array. I basically have this completed, but the NSRange is mysteriously changing it's value in the code, causing it to crash and throw an exception. Here is my code:
NSMutableArray*subStringArray = [[NSMutableArray alloc] init];
for(id key in aDict){
NSRange* range = CFBridgingRetain([aDict objectForKey:key]);
NSLog(#"This is a single range: %#",range);
//Range is changed here somehow
NSString* aSubString = [self.content substringWithRange:*range];
NSLog(#"%#",aSubString);
[subStringArray addObject:aSubString];
}
My Log output looks like this:
This is a single range: NSRange: {1874, 72}
2014-06-17 20:07:30.100 testApp[8027:60b] *** Terminating app due to uncaught exception
'NSRangeException', reason: '-[__NSCFString substringWithRange:]: Range {4318599072, 4} out of bounds; string length 5562'
By popular demand :)
The main issue with your code is that you are directly storing an NSRange struct in your dictionary. You can't do this. You must wrap the NSRange in an NSValue to store it in the dictionary.
To add the NSRange you can do:
NSRange range = ... // some range
NSValue *value = [NSValue valueWithRange:range];
dict[someKey] = value;
Then your posted code becomes:
for (id key in aDict) {
NSValue *value = aDict[key];
NSRange range = [value rangeValue];
NSLog(#"This is a single range: %#",range);
//Range is changed here somehow
NSString* aSubString = [self.content substringWithRange:range];
NSLog(#"%#",aSubString);
[subStringArray addObject:aSubString];
}

Looping through an array, index error

I am creating a UIScrollView that scrolls sideways and has a number of buttons and labels (that go with the buttons) that I am adding programmatically.
I have 44 buttons and labels, so I want to create them through an array.
This is how I am trying to do it (I'm a newish programmer so I know for a fact its the wrong way to do it.
Keep in mind, nameArray holds strings for the labels and the picArray holds strings for the filenames for pictures.
Anyways, this is in my viewDidLoad::
for (int i = 0; i < 44; i++) {
NSIndexPath *path = [nameArray objectAtIndex:i];
[self makeLabelsAndButtons:path];
//NSLog(#"index path is:%#", path);
}
The method that this for loop is referencing is written below:
-(void)makeLabelsAndButtons:(NSIndexPath*)indexPath{
NSString *nameString = [nameArray objectAtIndex:indexPath];
NSString *picString = [picArray objectAtIndex:indexPath];
NSLog(#"name: %#, pic:%#", nameString, picString);
}
There is not much in the makeLabelsAndButtons method because I am trying to get the concept down before adding the meat of the code.
This is the error I crash with:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 54564 beyond bounds [0 .. 43]
I know that this means the array is out of the bounds but I have no idea why.
Any help is appreciated!
If nameArray holds strings for the labels, then path is not an NSIndexPath, it's a string. So you're passing a string to objectAtIndex: which, of course wants an integer not a string.
Even if it were an NSIndexPath, your code would still be wrong because you want an integer not an index path to pass to objectAtIndex.
I think what you want to do is just pass i to your makeLabelsAndButtons method.
for (int i = 0; i < 44; i++) {
[self makeLabelsAndButtons:i];
//NSLog(#"index path is:%#", path);
}
-(void)makeLabelsAndButtons:(NSInteger)index{
NSString *nameString = [nameArray objectAtIndex:index];
NSString *picString = [picArray objectAtIndex:index];
NSLog(#"name: %#, pic:%#", nameString, picString);
}
Try like this...
either this...
for (int i = 0; i < [nameArray count]; i++) {
NSIndexPath *path = [nameArray objectAtIndex:i];
[self makeLabelsAndButtons:path];
}
or like this...
for (NSIndexPath *path in nameArray) {
[self makeLabelsAndButtons:path];
}

'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

I have an array with the names of the images in the form of strings, I want to transform it into an array of images and I getting this error, what I did wrong?
'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
My code:
NSString * immagini = self.chinaTable.images; //unique string
NSArray * arrayImages = [immagini componentsSeparatedByString:#";"];
NSLog(#"The images: %#", arrayImages);// here are strings
/*The images: (
"ArchUrb_PortaGenova1.jpg",
"ArchUrb_PortaGenova2.jpg",
"ArchUrb_PortaGenova3.jpg",
"ArchUrb_PortaGenova4.jpg"
)*/
NSMutableArray * mutableImages =[[NSMutableArray alloc]initWithCapacity:20];
for (id obj in arrayImages){
/*The images: (
"ArchUrb_PortaGenova1.jpg",
"ArchUrb_PortaGenova2.jpg",
"ArchUrb_PortaGenova3.jpg",
"ArchUrb_PortaGenova4.jpg"
)*/
[mutableImages addObject:[UIImage imageNamed:obj]];//here comes the error
NSLog(#"The array mutable is è %#", mutableImages);
}
NSLog(#"The array of images %#", mutableImages);
self.viewImages.animationImages = [NSArray arrayWithArray:mutableImages];
self.viewImages.animationDuration =3;
self.viewImages.animationRepeatCount= 0;
[self.viewImages startAnimating];
[UIImage imageNamed:] returns nil if it can't find the requested image in your app bundle.
Add missing image to your project, or add an if before adding image to array, like this:
for (id obj in arrayImages){
UIImage *image = [UIImage imageNamed:obj];
if (image != nil)
{
[mutableImages addObject:image];
}
NSLog(#"The array mutable is è %#", mutableImages);
}
Change your code segment as follows
for (id obj in arrayImages){
UIImage *image = [UIImage imageNamed:obj];
if ( image ) {
[mutableImages addObject:image];
NSLog(#"The array mutable is è %#", mutableImages);
}
}
This question is related to the question: Exception with insertObject:atIndex: on iOS6. So please read it to find your solution.

NSArray Error: NSCFArray objectAtIndex index (5) beyond bounds (5)

I have an MutableArray filled with values from a database:
NSMutableArray *objectsArray = [[NSMutableArray alloc] init];
for (int n=0; n<[self.array count]; n++) {
[objectsArray addObject:[[self.array objectAtIndex:n] objectForKey:#"Name"]];
[objectsArray addObject:[[self.array objectAtIndex:n] objectForKey:#"Name_En"]];
}
I show the values on labels like so:
cell.textLabel.text = [[[dic objectForKey:#"d"] objectAtIndex:indexPath.row] objectForKey:#"Name"];
cell.detailTextLabel.text = [[[dic objectForKey:#"d"] objectAtIndex:indexPath.row] objectForKey:#"Name_En"];
So far the app works fine. It starts without throwing an exception and the values are in the labels. But if I start to scroll the app crashes and I get the following error:
Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (5) beyond bounds (5)'
Why is this error occurring?
You are probably saying the table that you have 6 rows, but you actually have 5. The reason for this exception is that the table view asks for cell for indexPath.row with value of 5, which means it thinks that there are 6 [0:5] rows, but your array in the dictionary has 5 items [0:5).
I was facing the same the problem which you mention earlier.
Then I did this :-
cell.TitleLabel.text=[[AllUserDataArray objectAtIndex:indexPath.row]objectAtIndex:0];
cell.DateAndTimeLabel.text=[[AllUserDataArray objectAtIndex:indexPath.row]objectAtIndex:1];
cell.InfoLabel.text=[[AllUserDataArray objectAtIndex:indexPath.row]objectAtIndex:2];
if([[AllUserDataArray objectAtIndex:indexPath.row] count] >3) {
NSData *imageData=[[AllUserDataArray objectAtIndex:indexPath.row]objectAtIndex:3];
UIImage *travelImage=[UIImage imageWithData:imageData];
cell.ImgView.image=travelImage;
It is working fine and logically correct also(I think so) :)
Hope this will help you.
I was facing the same the problem and i got the solution:
NSMutableArray *objectsArray = [[NSMutableArray alloc] init];
objectsArray=nil;
for (int n=0; n<[self.array count]; n++) {
[objectsArray addObject:[[self.array objectAtIndex:n] objectForKey:#"Name"]];
[objectsArray addObject:[[self.array objectAtIndex:n] objectForKey:#"Name_En"]];
}

Resources