-[__NSArrayM objectAtIndex:]: index 11 beyond bounds [0 .. 10]' objective c - ios

Hi all getting issues with if(indexPath.row==3) section is
-[__NSArrayM objectAtIndex:]: index 11 beyond bounds [0 .. 10]'. Cannot get where the issue is exactly. Below is my code
if(indexPath.row==3)
{
cell.textLabel.text = #"Subject";
cell.detailTextLabel.text=[empDict objectForKey:#"Subject"];
}
if(indexPath.row==3)
{
cell.textLabel.text = #"Education";
cell.detailTextLabel.text=[empDict objectForKey:#"Education"];
}
NSArray *times = [empDict objectForKey:#"ClassTimings"];
NSString *stgmt = [[times objectAtIndex:indexPath.row]objectForKey:#"GMT"];
NSString *strtime = [[times objectAtIndex:indexPath.row]objectForKey:#"Time"];
NSString *strtimeZone = [[times objectAtIndex:indexPath.row]objectForKey:#"TimeZone"];
NSString *str = [#[stgmt, strtime, strtimeZone] componentsJoinedByString:#" "];
if(indexPath.row==3)
{
cell.textLabel.text = #"Times To Take";
cell.detailTextLabel.text=[NSString stringWithFormat:#"%#",str];
}
return cell;

Your problem is not in "if(indexPath.row==3)"
You're getting error from any of bellow three line
NSString *stgmt = [[times objectAtIndex:indexPath.row]objectForKey:#"GMT"];
NSString *strtime = [[times objectAtIndex:indexPath.row]objectForKey:#"Time"];
NSString *strtimeZone = [[times objectAtIndex:indexPath.row]objectForKey:#"TimeZone"];
where you are trying to access 12th element (when value of indexPath.row==12) of the 'times' array which don't have 12 elements...
Hope it helps..

This issue is because you've tried to access 12th element of an array of 11 elements.
In your code, if times array doesn't have 12 elements and if you are trying to access 12th element, this issue can occur. That is if indexpath.row = 12 and you try to access 12th object by [times objectAtIndex:indexPath.row]leads to this error.

Related

NSArray unrecognized selector sent to instance on Json

I am new to Objective-C And here I have a Json that I've stored into a NSArray. And I am trying to get the English, Dutch, Portuguese
{
English = (
One,
Two,
Three
);
Dutch = (
Een
);
Portuguese = (
Um,
Dois
}
And here's what I've done:
languageArray = result;
for (i = [languageArray count] - 1; i >= 0; i--)
{
NSString *languageTitle = [languageArray objectAtIndex:i];
NSLog(#"languageTitle %#", languageTitle);
}
And I have this error -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x7f8c68e53610 but my languageArray count is 3. I don't know why I can't get the objectAtIndex:i I've tried to use objectAtIndex:0 but still the same result
just use
NSArray*languages = [result allKeys];
for getting values for languages you have to do this
NSMutableArray *mutableArray = [[NSMutableArray alloc]init];
for (int i=0 ; i<languages.count ; i++){
[mutableArray addObject:[result valueForKey:[languages objectAtIndex:indexpath.row]]];
}
So finally you have mutable array which contains values for every language as array;
You can get values for english with respect to index of language.
For example you can get values for Dutch as
NSArray *dutchValues = [mutableArray objectAtIndex:1];

iOS NSArray count

I think my question is simple but it stumped me. Basically I have a NSArray of objects for example:
NSArray *arrayT = [NSArray arrayWithObjects:#"A", #"B", #"C", #"D", nil];
I want to put these objects into a TableViewCell. To display, I did
cell.positioninarray.text = [arrayT objectAtIndex:indexPath.row];
cell.letterinarray.text = [arrayT objectAtIndex:indexPath.row];
I am expecting this result
1 A
2 B
3 C
4 D
But am getting
A A
B B
C C
D D
I've looked through the NSArray help file in Apple developer website, but i don't see one that works for me.
Please help! Thanks!
cell.positioninarray.text = [NSString stringWithFormat:#"%d", (int)(indexPath.row+1)];
Replace
cell.positioninarray.text = [arrayT objectAtIndex:indexPath.row];
with
cell.positioninarray.text = [NSString stringWithFormat#"%d", ((int)indexPath.row+1)];
Explanation
You are setting the text for both positioninarray and letterinarray to be
[arrayT objectAtIndex:indexPath.row];
So you are getting A, B, C, D in both of the labels. To get 1, 2, 3, 4 in positioninarray, use the above code.
Change your code as below, indexPath.row will give you current index for the cell, and you want position start by 1 not 0 so indexPath.row + 1 will do your work, and finally convert your int value to string using NSString stringWithFormat :
cell.positioninarray.text = [NSString stringWithFormat:#"%i", (indexPath.row + 1)];
cell.letterinarray.text = [arrayT objectAtIndex:indexPath.row];

NSDecimalNumbers in Array Causing "Unrecognized Selector..." messages

Ive been playing around with obj-c for a few months now and feel comfortable with a lot of things but cant make sense of something that seems very simple. I need to be able to go from an NSString split into an array then get the NSDecimalNumbers from that array. If I try to perform any calculations, I get an "unrecognized selector sent to instance." If I simply print each decimal number everything goes smoothly. If I put the values manually into NSDecimalNumbers rather than pulling from the array that also works. The error and code are below.
2013-10-14 15:54:42.174 test[30829:c07] -[__NSCFString
decimalNumberByAdding:]: unrecognized selector sent to instance
0x75a81f0 2013-10-14 15:54:42.176 test[30829:c07] * Terminating app
due to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSCFString decimalNumberByAdding:]: unrecognized selector sent to
instance 0x75a81f0'
* First throw call stack: (0x1c90012 0x10cde7e 0x1d1b4bd 0x1c7fbbc 0x1c7f94e 0x2075 0x10e1705 0x152c0 0x15258 0xd6021 0xd657f 0xd56e8
0x44cef 0x44f02 0x22d4a 0x14698 0x1bebdf9 0x1bebad0 0x1c05bf5
0x1c05962 0x1c36bb6 0x1c35f44 0x1c35e1b 0x1bea7e3 0x1bea668 0x11ffc
0x1bfd 0x1b25) libc++abi.dylib: terminate called throwing an exception
-(IBAction)buttonPushed: (id) sender
{
NSString* s = #"25.55, 109.24";
NSArray* a = [s componentsSeparatedByString: #" "];
NSDecimalNumber* dec1 = [a objectAtIndex: 0];
NSDecimalNumber* dec2 = [a objectAtIndex: 1];
NSDecimalNumber* sum = [dec1 decimalNumberByAdding: dec2];
statusText.text = [NSString stringWithFormat: #"%#", sum, nil];
}
NSDecimalNumber* dec1 = [a objectAtIndex: 0];
doesn't give you a NSDecimalNumber object. It gives you a string (which was created via the call to componentsSeparatedByString)
Try doing this:
NSDecimalNumber* dec1 = [NSDecimalNumber decimalNumberWithString: [a objectAtIndex: 0]];
and see if you have better luck.
you never created a NSDecimalNumber object
NSArray* a = [s componentsSeparatedByString: #", "];
will of course create an aray of strings.
Just because you type the pointer as decimal number, doesnt make the obejct to be one
NSDecimalNumber* dec1 = [a objectAtIndex: 0];
dec1 is still a NSString. Try
NSDecimalNumber* dec1 = [NSDecimalNumber decimalNumberWithString:[a objectAtIndex: 0]];
NSDecimalNumber* dec2 = [NSDecimalNumber decimalNumberWithString:[a objectAtIndex: 1]];
NSDecimalNumber* sum = [dec1 decimalNumberByAdding: dec2];
statusText.text = [NSString stringWithFormat: #"%#", sum];
When you call [a objectAtIndex: 0] that is just returning a string, that you're trying to cast to a NSDecimalNumber.
Instead, try using something like this:
-(IBAction)buttonPushed: (id) sender
{
NSString* s = #"25.55, 109.24";
NSArray* a = [s componentsSeparatedByString: #", "];
NSDecimalNumber* dec1 = [NSDecimalNumber decimalNumberWithString:[a objectAtIndex: 0]];
NSDecimalNumber* dec2 = [NSDecimalNumber decimalNumberWithString:[a objectAtIndex: 1]];
NSDecimalNumber* sum = [dec1 decimalNumberByAdding: dec2];
statusText.text = [NSString stringWithFormat: #"%#", sum, nil];
}
Also note that I'm splitting the string with ", " and not just " ", otherwise the first string will be "25.55,"
See the NSDecimalNumber documentation for the details: https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSDecimalNumber_Class/Reference/Reference.html#//apple_ref/occ/clm/NSDecimalNumber/decimalNumberWithString:

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];
}

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