-2

I have initialize NSMutableArray in viewDidLoad like

playUrl = [[NSMutableArray alloc]init];

But when we use in cellForRowAtIndexPath

[playUrl insertObject:[NSString stringWithFormat:@"%@",_urlToLoad] atIndex:indexPath.row];

Then it will be show this error

2016-11-21 18:06:14.481 TraidingWins[13917:238534] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM insertObject:atIndex:]: index 1 beyond bounds for empty array'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000106348d85 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000105dbcdeb objc_exception_throw + 48
    2   CoreFoundation                      0x0000000106209cc5 -[__NSArrayM insertObject:atIndex:] + 901
    3   TraidingWins                        0x0000000102dc7bd6 __45-[YouTubeVC tableView:cellForRowAtIndexPath:]_block_invoke_2 + 902
    4   TraidingWins                        0x0000000102dbb840 __58+[HCYoutubeParser h264videosWithYoutubeURL:completeBlock:]_block_invoke_2 + 48
    5   libdispatch.dylib                   0x000000010705fd9d _dispatch_call_block_and_release + 12
    6   libdispatch.dylib                   0x00000001070803eb _dispatch_client_callout + 8
    7   libdispatch.dylib                   0x00000001070681ef _dispatch_main_queue_callback_4CF + 1738
    8   CoreFoundation                      0x00000001062a20f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    9   CoreFoundation                      0x0000000106263b99 __CFRunLoopRun + 2073
    10  CoreFoundation                      0x00000001062630f8 CFRunLoopRunSpecific + 488
    11  GraphicsServices                    0x00000001087f5ad2 GSEventRunModal + 161
    12  UIKit                               0x0000000104435f09 UIApplicationMain + 171
    13  TraidingWins                        0x0000000102dcba8f main + 111
    14  libdyld.dylib                       0x00000001070b492d start + 1
    15  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Can anybody help me for solving the crashing issues..

8
  • 1
    Please check your array is empty, that's why you having crash, Print array where you are processing your playUrl Commented Nov 21, 2016 at 12:40
  • read the error message, fixing the error is basically as simple as that. Commented Nov 21, 2016 at 12:41
  • never ever do array insert and delete operation in cellForRowAtIndexPath alway follow the best practices Commented Nov 21, 2016 at 12:42
  • Yes, My array is empty, i have not get value from array, Acutally i want to insert data . So i don't understand why it is crash. Commented Nov 21, 2016 at 12:42
  • 1
    @HariMohan it crashes because you can't jump over missing positions when you add objects to an array. If the array is empty the only position you may insert to is 0. You insert to 1'st position it crashes. Commented Nov 21, 2016 at 14:21

3 Answers 3

0

Add your static data into array just after initialization. If you add or insert in UITableView methods you will get crashes. Do it this way and check response

Sign up to request clarification or add additional context in comments.

Comments

0

There're 2 possible solutions:

1. You can use NSMutableDictionary.

Initialization:

playUrl = [[NSMutableDictionary alloc] init];

In cellForRowAtIndexPath:

playUrl[@(indexPath.row)] = [NSString stringWithFormat:@"%@", _urlToLoad];

2. Seems like your UITableView is initially scrolled a bit. So the first cell it requests is 1'st (not 0'th as you expect). If it was 0'th, the crash would go away.

You may insert an object to NSMutableArray only to available positions. For example, if your array contains 3 objects, you may insert to 0...3 (including 0 and 3).

UPDATE

Every time you're about to insert an object to the array, you need to check if there's already something at the index. If there's an object, instead of inserting, you should replace (replaceObjectAtIndex:withObject:). Otherwise, order would be corrupted.

Example

a user scrolls down to url3

@[ @"url1", @"url2", @"url3" ]  // everything is fine

then scrolls up. And table requests url2 again.

@[ @"url1", @"url2", @"url2", @"url3" ]  // second url2 and url3 occupy wrong positions

The issue happens if you always insert objects. If you replaced url2 with url2, everything would be ok, the array wouldn't change.

Comments

-1

I guess you are tapping at Second Object means objectAtInxex 1 so ,There are no object at 1st index and you are directly inserting object at 1st index. In my opinion if you are adding or replacing only list of urlobjects you can use addObject(addbojectAtIndex Specific index). Also let me know if anything you want to know specifically. Many Thanks :)

1 Comment

1) cellForRowAtIndexPath has nothing to do with tapping. 2) addObject is not good enough, if you scroll up, urls won't correspond to indices; 3) addbojectAtIndex (correct is insertObject:atIndex:) is used in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.