NSArrayM objectAtIndex: index 6 beyond bounds - ios

I am developing iOS application. Value store on Array, but when I run app, its crashes with array index out of bounds. Please Help and Thanks in advance..
Code:
daysOfWeek = [NSArray arrayWithObjects:#"Monday",#"Tuesday",#"Wednesday",#"Thursday",#"Friday",#"Saturday",#"Sunday", nil];
for (int i = 0; i<= [_daysOfWeek count]; i++) {
_mondayView=[[UIView alloc]initWithFrame:CGRectMake(45, 300 , 830, 30.0)];
[_mondayView setBackgroundColor:[UIColor colorWithRed:190.0/255.0 green:190.0/255.0 blue:194.0/255.0 alpha:1.0]];
[_weeklyView addSubview:_mondayView];
UILabel *daysLabel = [[UILabel alloc]initWithFrame:CGRectMake(65.0f, 300.0f, 120.0f, 30.0f)];
daysLabel.backgroundColor = [UIColor blueColor];
daysLabel.text = [NSString stringWithFormat:#"%#",[_daysOfWeek objectAtIndex:i]];
[_weeklyView addSubview:daysLabel];
}
Crash Log:
2016-11-14 11:48:41.266 FH Harvey[1839:67640] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 7 beyond bounds [0 .. 6]'
*** First throw call stack:
(
0 CoreFoundation 0x000000011101934b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000011065d21e objc_exception_throw + 48
2 CoreFoundation 0x0000000110f53eeb -[__NSArrayI objectAtIndex:] + 155
3 FH Harvey 0x000000010fe1ee62 -[FHWeeklyViewController viewDidLoad] + 1026
4 UIKit 0x00000001115ddc99 -[UIViewController loadViewIfRequired] + 1258
5 UIKit 0x00000001115e4102 -[UIViewController __viewWillAppear:] + 118
6 UIKit 0x000000011160efbf -[UINavigationController _startCustomTransition:] + 1290
7 UIKit 0x000000011161fc34 -[UINavigationController _startDeferredTransitionIfNeeded:] + 697
8 UIKit 0x0000000111620dc7 -[UINavigationController __viewWillLayoutSubviews] + 58
9 UIKit 0x0000000111817d6f -[UILayoutContainerView layoutSubviews] + 223
10 UIKit 0x0000000111500f50 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
11 QuartzCore 0x000000010ffbdcc4 -[CALayer layoutSublayers] + 146
12 QuartzCore 0x000000010ffb1788 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
13 QuartzCore 0x000000010ffb1606 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
14 QuartzCore 0x000000010ff3f680 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
15 QuartzCore 0x000000010ff6c767 _ZN2CA11Transaction6commitEv + 475
16 UIKit 0x0000000111435a97 _UIApplicationFlushRunLoopCATransactionIfTooLate + 206
17 UIKit 0x0000000111c456e0 __handleEventQueue + 5672
18 CoreFoundation 0x0000000110fbe311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
19 CoreFoundation 0x0000000110fa359c __CFRunLoopDoSources0 + 556
20 CoreFoundation 0x0000000110fa2a86 __CFRunLoopRun + 918
21 CoreFoundation 0x0000000110fa2494 CFRunLoopRunSpecific + 420
22 GraphicsServices 0x00000001163a0a6f GSEventRunModal + 161
23 UIKit 0x000000011143c964 UIApplicationMain + 159
24 FH Harvey 0x000000010fe221bf main + 111
25 libdyld.dylib 0x000000011491368d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

The problem is with your for loop. Array index start with 0 so change your for loop to:
for (int i = 0; i < [_daysOfWeek count]; i++) {
You are using i<=[_daysOfWeek count] that is the reason you are getting index 7 beyond bounds [0 .. 6] crash.
Instead of using for loop you can use for each loop and it easy to implement and it will never cause you index out of bound crash.
for (NSString* str in _daysOfWeek) {
NSLog(#"%#",str);
}

DEEPAK kumar just verify your logic. Your array having 7 values and you are iterating it 8 times. Just change the logic
for (int i = 0; i < [_daysOfWeek count]; i++)
or
use for each loop as Nirav D describes (most optimized way).
Happy coding..

Just changing you for loop logic from i <= [_daysOfWeek count] to i < [_daysOfWeek count] should resolve your problem. However, to avoid mistakes like this in future, I suggest you to use enumerateObjectsUsingBlock method of NSArray. It can be done in the following way:
NSArray *daysOfWeek = [NSArray arrayWithObjects:#"Monday",#"Tuesday",#"Wednesday",#"Thursday",#"Friday",#"Saturday",#"Sunday", nil];
[daysOfWeek enumerateObjectsUsingBlock:^(NSString* _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
}];

Please write :
for (int i = 0; i < [_daysOfWeek count]; i++)
It will help you.

Related

I am getting error at my cellForRowAtIndexPath in tableview after i filter with predicate

[__NSDictionaryM safeObjectAtIndex:]: unrecognized selector sent to instance 0x60800005e060
[__NSDictionaryM safeObjectAtIndex:]: unrecognized selector sent to instance 0x60800005e060 [IssuesViewController-tableView:cellForRowAtIndexPath:]
Assertion failure in -[STCollapseTableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.7.47/UITableView.m:8174
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (; layer = ; contentOffset: {0, 0}; contentSize: {414, 115}>) failed to obtain a cell from its dataSource (; layer = ; contentOffset: {0, 0}; contentSize: {414, 115}>)'
*** First throw call stack:
(
0 CoreFoundation 0x0000000108d8bb0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001087c3141 objc_exception_throw + 48
2 CoreFoundation 0x0000000108d8fcf2 +[NSException raise:format:arguments:] + 98
3 Foundation 0x000000010835d3b6 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
4 UIKit 0x0000000109aeefcf -[UITableView _configureCellForDisplay:forIndexPath:] + 230
5 UIKit 0x0000000109afa7b8 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 836
6 UIKit 0x0000000109afa9a8 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
7 UIKit 0x0000000109acf2e9 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2845
8 UIKit 0x0000000109b0397c -[UITableView _performWithCachedTraitCollection:] + 111
9 UIKit 0x0000000109aeab2a -[UITableView layoutSubviews] + 233
10 UIKit 0x0000000109a5120b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1268
11 QuartzCore 0x0000000107905904 -[CALayer layoutSublayers] + 146
12 QuartzCore 0x00000001078f9526 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 370
13 QuartzCore 0x00000001078f93a0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
14 QuartzCore 0x0000000107888e92 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
15 QuartzCore 0x00000001078b5130 _ZN2CA11Transaction6commitEv + 468
16 UIKit 0x0000000109987307 _UIApplicationFlushRunLoopCATransactionIfTooLate + 167
17 UIKit 0x000000010a187cab __handleEventQueue + 5843
18 CoreFoundation 0x0000000108d31c01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
19 CoreFoundation 0x0000000108d170cf __CFRunLoopDoSources0 + 527
20 CoreFoundation 0x0000000108d165ff __CFRunLoopRun + 911
21 CoreFoundation 0x0000000108d16016 CFRunLoopRunSpecific + 406
22 GraphicsServices 0x000000010b8eaa24 GSEventRunModal + 62
23 UIKit 0x000000010998e0d4 UIApplicationMain + 159
24 comress 0x000000010626910f main + 111
25 libdyld.dylib 0x000000010b4d765d start + 1)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
This is sample code:
NSArray *postDupe = [[_postsArray valueForKeyPath:#"#unionOfArrays.#allValues"] valueForKeyPath:#"post"];
NSArray *foundArray = nil;
predicate = [NSPredicate predicateWithFormat:#"post_id.stringValue contains [c] %#",#"18586"];
foundArray = [postDupe filteredArrayUsingPredicate:predicate];
long foundIndex = 0;
long count = 0;
for (int i=0; i<foundArray.count; i++)
{
foundIndex = [postDupe indexOfObject: [foundArray objectAtIndex: i]];
//NSLog(#"%# is at index %d", string, index);
}
_filterPostsArray = [_postsArray objectAtIndex:foundIndex];
}
searchActive = YES;
dispatch_async(dispatch_get_main_queue(),^{
[_issuesTable reloadData];
});
Sample Json Data:
<__NSArrayM 0x608000449a80>(
{
14556 = {
newCommentsCount = 0;
post = {
"post_id" = 18609;
seen = 1;
status = 0;
statusWasUpdated = 0;
"updated_on" = "1506061049.023";
};
};
},
)
[__NSDictionaryM safeObjectAtIndex:]: unrecognized selector sent to instance
Looks like you have a dictionary and are treating it like an array.
Run your code in the debugger and examine the values of the variables as you move through your algorithm, this should show you where you go wrong.
Guessing:
The key path in your predicate post_id.stringValue should be post.post_id.stringValue - your dictionary contains a key post whose value is itself a dictionary which has a key post_id.
_filterPostsArray = [_postsArray objectAtIndex:foundIndex] sounds like it is assigning a dictionary to an array. You probably wanted this statement to be in the loop and adding the dictionary as a new element to the array?
HTH

Trying to Append Retweeted Tweet

I am trying to append the string of a Retweeted tweet in Objective-C however I am always getting an issue with certain types of tweets. I believe that there is something wrong in the way I am attempting to substring the tweets. My code is shown below
2015-08-20 15:40:20.699 Floadt[21417:932559] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString substringWithRange:]: Range {9223372036854775807, 52} out of bounds; string length 70'
*** First throw call stack:
(
0 CoreFoundation 0x000000010cd58c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010c631bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010cd58b9d +[NSException raise:format:] + 205
3 CoreFoundation 0x000000010cc7fb98 -[__NSCFString substringWithRange:] + 136
4 Floadt 0x000000010929efa4 -[TwitterTableViewController tableView:cellForRowAtIndexPath:] + 2260
5 UIKit 0x000000010b465a28 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
6 UIKit 0x000000010b444248 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2853
7 UIKit 0x000000010b45a8a9 -[UITableView layoutSubviews] + 210
8 UIKit 0x000000010b3e4a2b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
9 QuartzCore 0x000000010b1a6ec2 -[CALayer layoutSublayers] + 146
10 QuartzCore 0x000000010b19b6d6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
11 QuartzCore 0x000000010b19b546 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
12 QuartzCore 0x000000010b107886 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
13 QuartzCore 0x000000010b108a3a _ZN2CA11Transaction6commitEv + 462
14 QuartzCore 0x000000010b1090eb _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
15 CoreFoundation 0x000000010cc8bca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
16 CoreFoundation 0x000000010cc8bc00 __CFRunLoopDoObservers + 368
17 CoreFoundation 0x000000010cc81a33 __CFRunLoopRun + 1123
18 CoreFoundation 0x000000010cc81366 CFRunLoopRunSpecific + 470
19 GraphicsServices 0x000000010eb09a3e GSEventRunModal + 161
20 UIKit 0x000000010b364900 UIApplicationMain + 1282
21 Floadt 0x000000010936574f main + 111
22 libdyld.dylib 0x000000010f32f145 start + 1
23 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Here is my code to Append the string:
if ([self Contains:#"RT" on:[data objectForKey:#"text"]]) {
TwitterRetweetCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"TwitterRetweetCell"];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Background.png"]];
if (cell == nil) {
cell = [[TwitterRetweetCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"TwitterRetweetCell"];
}
// Lookup RT User
NSString *text = [data objectForKey:#"text"];
NSRange start = [text rangeOfString:#"RT #"];
NSRange end = [text rangeOfString:#":"];
NSString *shortString = [text substringWithRange:NSMakeRange(start.location, end.location)];
NSString *evenShorterString = [shortString substringFromIndex:4];
[self lookupTwitterUser:evenShorterString];
//Set username for twitter
[cell.nameLabel setText:evenShorterString];
//Set Retweet Status
NSString *retName = [NSString stringWithFormat:#"Retweeted by %#",data[#"user"][#"screen_name"]];
[cell.retweetedLabel setText:retName];
//Set Profile Pic for Twitter
return cell;
}
The two ranges:
NSRange start = [text rangeOfString:#"RT #"];
NSRange end = [text rangeOfString:#":"];
You need to check that those substrings were actually found and only use the ranges if they were found. Otherwise the rest of your code will fail because the range's location will be NSNotFound, which is the large number in your crash log. That suggests that your code encountered text containing "RT" but not "RT #". You should perform the following check before attempting to use start and end:
if (start.location != NSNotFound && end.location != NSNotFound)
{
// use start and end
}
Or better still, use this if condition instead of if ([self Contains:#"RT" on:[data objectForKey:#"text"]]).

Adding null object to SQLite

I implemented SQLite in xcode. I have 2 columns. When the first column has a row, then the second one is set to null, and vice versa. I'm trying to go through both columns and check if it has an object at that index.
NSLog(#"%# and %#", firstArray, secondArray); // It gives the right risults, but there aren't any null objects in any of them.
for (int i = 0; i < [firstArray count]; i++)
{
if (![[firstArray objectAtIndex:i] length] && [[secondArray objectAtIndex:i] length]) {
...
}
}
I have two issues.
First: There aren't any null objects in the rows, even though I set some to null. So what should I do to check if there is a null object, or if there's another way to add some kind null object that will actually add to the tables row?
Second: When I compile and run the app, it crashes with the error below. But when I remove the if statement, it works flawlessly.
-[__NSArrayM length]: unrecognized selector sent to instance 0x7fbfeb53b0a0
2015-03-30 11:25:04.731 MyApp [610:15600] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM length]: unrecognized selector sent to instance 0x7fbfeb53b0a0'
*** First throw call stack:
(
0 CoreFoundation 0x0000000110874f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000011050dbb7 objc_exception_throw + 45
2 CoreFoundation 0x000000011087c04d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00000001107d427c ___forwarding___ + 988
4 CoreFoundation 0x00000001107d3e18 _CF_forwarding_prep_0 + 120
5 MyApp 0x000000010fec3b59 -[ViewController ingredientAction] + 889
6 MyApp 0x000000010feba253 -[ViewController menuAction:] + 195
7 UIKit 0x0000000110ea58be -[UIApplication sendAction:to:from:forEvent:] + 75
8 UIKit 0x0000000110fac410 -[UIControl _sendActionsForEvents:withEvent:] + 467
9 UIKit 0x00000001110269ea -[UISegmentedControl _setSelectedSegmentIndex:notify:animate:] + 570
10 UIKit 0x0000000111028a0f -[UISegmentedControl touchesEnded:withEvent:] + 143
11 UIKit 0x0000000111252540 _UIGestureRecognizerUpdate + 9487
12 UIKit 0x0000000110eeaff6 -[UIWindow _sendGesturesForEvent:] + 1041
13 UIKit 0x0000000110eebc23 -[UIWindow sendEvent:] + 667
14 UIKit 0x0000000110eb89b1 -[UIApplication sendEvent:] + 246
15 UIKit 0x0000000110ec5a7d _UIApplicationHandleEventFromQueueEvent + 17370
16 UIKit 0x0000000110ea1103 _UIApplicationHandleEventQueue + 1961
17 CoreFoundation 0x00000001107aa551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
18 CoreFoundation 0x00000001107a041d __CFRunLoopDoSources0 + 269
19 CoreFoundation 0x000000011079fa54 __CFRunLoopRun + 868
20 CoreFoundation 0x000000011079f486 CFRunLoopRunSpecific + 470
21 GraphicsServices 0x0000000114c759f0 GSEventRunModal + 161
22 UIKit 0x0000000110ea4420 UIApplicationMain + 1282
23 MyApp 0x000000010fec11b3 main + 115
24 libdyld.dylib 0x00000001122ad145 start + 1
25 ??? 0x0000000000000001 0x0 + 1
Edit
I put #"" instead of null into the row. Then I change the if statement (in the for loop) to the following:
NSStirng *objectAtIndexFromArray = [NSString stringWithFormat:#"%#", [firstArray objectAtIndex:i]];
if ([objectAtIndexFromArray isEqualToString:#""])
{
NSLog(#"Hello");
}
I don't get any NSLogs of Hello.
I'm pretty new to ios development, but I know that if you want to add null object to you data collection you have to use NSNULL class.
NSArray can't hold nils, if you want to check if null, please use this code
id object = myArray[0];// similar to [myArray objectAtIndex:0]
if(![object isEqual:[NSNull null]])
{
//do something if object is not equals to [NSNull null]
}

Access Keys after adding NSDictionary to NSMutableArray. iOS

I have a NSDictionary that I add to a mutable array but when I try add the array to populate a uitableview I get an error.
NSMutableArray *array = [[NSMutableArray alloc] init];
for (id element in self.categoriesMutableNameArray) {
[array addObject:#"No"];
}
self.categoryDict = #{ #"title" : self.categoriesMutableNameArray, #"selected" : array};
self.categoryArr = [[NSMutableArray alloc] init];
self.categoryMutableDict = [NSMutableDictionary dictionaryWithDictionary:self.categoryDict];
[self.categoryArr addObject:self.categoryDict];
and the following categoryArr is printed like this:
2014-02-27 15:09:07.397 App[7982:70b] (
{
selected = (
No,
No,
No,
No,
No,
No,
No,
No,
No,
No,
No,
No,
No,
No
);
title = (
"Fashion - Women",
"Fashion - Men",
Kids,
"Accessories - Women",
"Accessories - Men",
"Styling / Hair",
Inspiration,
"Decoration / Architecture",
"Great Places",
"Art / Design",
"Music / Movie / Books",
"Food / Drink",
"Gadgets / Tech",
Rides
);
}
)
The trouble I am having is in the uitableview cellforrowatindexpath method I try and add the title key for the categoryArr to populate the uitableview and I get the following error on this line:
UILabel *categoryLabel = (UILabel *)[cell viewWithTag:111];
categoryLabel.text = [NSString stringWithFormat:#"%#",[[self.categoryArr objectAtIndex:indexPath.row] objectForKey:#"title"];
And the error log:
2014-02-27 15:24:33.804 App[8153:70b] -[__NSArrayM length]: unrecognized selector sent to instance 0xa9bc3a0
2014-02-27 15:24:33.807 App[8153:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM length]: unrecognized selector sent to instance 0xa9bc3a0'
*** First throw call stack:
(
0 CoreFoundation 0x020c75e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01e4a8b6 objc_exception_throw + 44
2 CoreFoundation 0x02164903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x020b790b ___forwarding___ + 1019
4 CoreFoundation 0x020b74ee _CF_forwarding_prep_0 + 14
5 Foundation 0x006e18ed -[NSConcreteMutableAttributedString replaceCharactersInRange:withString:] + 39
6 Foundation 0x006e255a -[NSConcreteMutableAttributedString initWithString:attributes:] + 293
7 UIKit 0x01172bc6 -[UILabel _setText:] + 97
8 UIKit 0x01172d84 -[UILabel setText:] + 40
9 App 0x00047ebd -[PiccImageCategoriesViewController tableView:cellForRowAtIndexPath:] + 1533
10 UIKit 0x010b461f -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412
11 UIKit 0x010b46f3 -[UITableView _createPreparedCellForGlobalRow:] + 69
12 UIKit 0x01098774 -[UITableView _updateVisibleCellsNow:] + 2378
13 UIKit 0x010abe95 -[UITableView layoutSubviews] + 213
14 UIKit 0x01030267 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
15 libobjc.A.dylib 0x01e5c81f -[NSObject performSelector:withObject:] + 70
16 QuartzCore 0x00c8e2ea -[CALayer layoutSublayers] + 148
17 QuartzCore 0x00c820d4 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
18 QuartzCore 0x00c81f40 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
19 QuartzCore 0x00be9ae6 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
20 QuartzCore 0x00beae71 _ZN2CA11Transaction6commitEv + 393
21 QuartzCore 0x00beb544 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
22 CoreFoundation 0x0208f4ce __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
23 CoreFoundation 0x0208f41f __CFRunLoopDoObservers + 399
24 CoreFoundation 0x0206d344 __CFRunLoopRun + 1076
25 CoreFoundation 0x0206cac3 CFRunLoopRunSpecific + 467
26 CoreFoundation 0x0206c8db CFRunLoopRunInMode + 123
27 GraphicsServices 0x027779e2 GSEventRunModal + 192
28 GraphicsServices 0x02777809 GSEventRun + 104
29 UIKit 0x00fc5d3b UIApplicationMain + 1225
30 App 0x0001406d main + 141
31 libdyld.dylib 0x02f4d70d start + 1
32 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Thanks
The return of [[self.categoryArr objectAtIndex:indexPath.row] objectForKey:#"title"]; is a array of NSString, categoryLabel.text = [NSString stringWithFormat:#"%#", /*the return array*/]; this is the problem.
Maybe what you want is:
NSArray *categoryDetailArr = [[self.categoryArr objectAtIndex:indexPath.section] objectForKey:#"title"];
categoryLabel.text = [[categoryDetailArr objectAtIndex:indexPath.row];
Write this code
categoryLabel.text = [[[self.categoryArr objectAtIndex:0] objectForKey:#"title"]objectAtIndex:indexPath.row];
instead OF
categoryLabel.text = [NSString stringWithFormat:#"%#",[[self.categoryArr objectAtIndex:indexPath.row] objectForKey:#"title"];
And plz check that in UITableView dataSource methods i.e
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [categoryLabel objectAtIndex:0]valueForKey:#"selected"].count;
}
OR
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [categoryLabel objectAtIndex:0]valueForKey:#"title"].count;
}

Terminating app due to uncaught exception 'NSRangeException' xcode 5

I got this error in a picker view function that getting its data to a text file from google spreadsheet.. this is the code of the function.
{
- (IBAction)clickText:(id)sender
int tag = [(UITextField*)sender tag];
if (tag!=11)
if (tag!=12)
if (tag!=13)
if (tag!=14)
if (tag!=15)
if (tag!=16)
if (tag!=17)
if (tag!=18)
if (tag!=19)
{
[sender resignFirstResponder];
}
self.myPicker.hidden = NO;
selectedTable = tag;
[sender resignFirstResponder];
float yy = 10;
switch (tag) {
case 11: yy = self.txtTgfeet.frame.origin.y + self.myPicker.frame.size.height;
break;
case 12: yy = self.txtTginches.frame.origin.y + self.myPicker.frame.size.height;
break;
case 13: yy = self.txtTgfraction.frame.origin.y + self.myPicker.frame.size.height;
break;
case 14: yy = self.txtBgfeet.frame.origin.y - self.myPicker.frame.size.height;
break;
case 15: yy = self.txtBginches.frame.origin.y - self.myPicker.frame.size.height;
break;
case 16: yy = self.txtBgfraction.frame.origin.y - self.myPicker.frame.size.height;
break;
case 17: yy = self.txtGravity.frame.origin.y - self.myPicker.frame.size.height;
break;
case 18: yy = self.txtBsw.frame.origin.y - self.myPicker.frame.size.height;
break;
case 19: yy = self.txtTemp.frame.origin.y - self.myPicker.frame.size.height;
break;
default:
break;
}
[self.myPicker setFrame:CGRectMake(self.txtTgfeet.frame.origin.x, yy, self.txtTgfeet.frame.size.width, 216)];
[self.ar removeAllObjects];
NSString *theURL = #"https://docs.google.com/spreadsheet/pub?key=0AobIgii9hBGPdDJ3TmgxSkRzVFBKWXVnTWdQcGtyMFE&single=true&gid=9&output=txt";
NSString *theFile = [NSString stringWithContentsOfURL:[NSURL URLWithString:theURL] encoding:NSUTF8StringEncoding error:nil];
NSLog(#"%#",theFile);
NSArray *theCells = [theFile componentsSeparatedByString:#"\n"];
for (int i=1; i < [theCells count]; i++)
{
// if (i > 0)
{
NSArray *value =[[theCells objectAtIndex:i] componentsSeparatedByString:#"\t"];
[self.ar addObject:[value objectAtIndex:tag+1]];
}
}
[self.myPicker selectRow:0 inComponent:0 animated:NO];
[self.myPicker reloadAllComponents];
This function is working perfectly on the other app, I've made this only for another app that I want this feature. Now when I click the text field I am getting this error:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 12 beyond bounds [0 .. 8]'
*** First throw call stack:
(
0 CoreFoundation 0x01b575e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x018da8b6 objc_exception_throw + 44
2 CoreFoundation 0x01af84e6 -[__NSArrayM objectAtIndex:] + 246
3 TGC 0x000080ba -[MainViewController clickText:] + 4602
4 libobjc.A.dylib 0x018ec81f -[NSObject performSelector:withObject:] + 70
5 UIKit 0x0043d0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
6 UIKit 0x0043d04e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
7 UIKit 0x005350c1 -[UIControl sendAction:to:forEvent:] + 66
8 UIKit 0x00535484 -[UIControl _sendActionsForEvents:withEvent:] + 577
9 UIKit 0x00b222ae -[UITextField willAttachFieldEditor:] + 685
10 UIKit 0x0053aeb5 -[UIFieldEditor becomeFieldEditorForView:] + 927
11 UIKit 0x00b193a3 -[UITextField _becomeFirstResponder] + 160
12 UIKit 0x00594ef5 -[UIResponder becomeFirstResponder] + 400
13 UIKit 0x0049819b -[UIView(Hierarchy) becomeFirstResponder] + 114
14 UIKit 0x00b18e43 -[UITextField becomeFirstResponder] + 51
15 UIKit 0x007bd551 -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setFirstResponderIfNecessary] + 135
16 UIKit 0x007bfaa2 -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) oneFingerTap:] + 2640
17 UIKit 0x007b3e8c _UIGestureRecognizerSendActions + 230
18 UIKit 0x007b2b00 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 383
19 UIKit 0x007b456d -[UIGestureRecognizer _delayedUpdateGesture] + 60
20 UIKit 0x007b7acd ___UIGestureRecognizerUpdate_block_invoke + 57
21 UIKit 0x007b7a4e _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 317
22 UIKit 0x007ae148 _UIGestureRecognizerUpdate + 199
23 UIKit 0x0047a19a -[UIWindow _sendGesturesForEvent:] + 1291
24 UIKit 0x0047b0ba -[UIWindow sendEvent:] + 1030
25 UIKit 0x0044ee86 -[UIApplication sendEvent:] + 242
26 UIKit 0x0043918f _UIApplicationHandleEventQueue + 11421
27 CoreFoundation 0x01ae083f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
28 CoreFoundation 0x01ae01cb __CFRunLoopDoSources0 + 235
29 CoreFoundation 0x01afd29e __CFRunLoopRun + 910
30 CoreFoundation 0x01afcac3 CFRunLoopRunSpecific + 467
31 CoreFoundation 0x01afc8db CFRunLoopRunInMode + 123
32 GraphicsServices 0x031bb9e2 GSEventRunModal + 192
33 GraphicsServices 0x031bb809 GSEventRun + 104
34 UIKit 0x0043bd3b UIApplicationMain + 1225
35 TGC 0x00002d62 main + 130
36 libdyld.dylib 0x022fd725 start + 0
37 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
what is seems to be the problem here?
Here it is clearly mentioned that [__NSArrayM objectAtIndex:]: index 12 beyond bounds [0 .. 8]'
You are trying to access 12th [index 12 and array has 0 to 8 object only] element which is not present, so application is crashing.
Using break point check below line,
[self.ar addObject:[value objectAtIndex:tag+1]];
Your problem seems to be here:
[value objectAtIndex:tag+1]
You are accessing the index 12, when your array value only contains 9 elements.
NSArray *value =[[theCells objectAtIndex:i] componentsSeparatedByString:#"\t"];
if ([value count]>tag+1) {
[self.ar addObject:[value objectAtIndex:tag+1]];
}

Resources