1

I get the error:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSA

Here is my code:

static NSArray * clone(NSArray * a, NSUInteger b)
{
    // return a.slice(b);
    return [a subarrayWithRange:NSMakeRange(b, a.count - b)];
}
// swap: function(a, b) {
static NSArray * swap(NSArray *a, NSUInteger b)
{
    NSMutableArray * array = [NSMutableArray arrayWithArray:a];

// var t1, t2;
id t1, t2;

// t1 = a[0];
t1 = array.firstObject;

// t2 = a[b % a.length];
t2 = array[b % a.count];

// a[0] = t2;
array[1] = t2;

// a[b] = t1;
array[b] = t1;

// return a;
return array.copy;
}

I'm confused on why it is giving me the error. This is just a snippet of the code, This comes from a modified version of HCYoutubeParser.

5
  • 1
    Read the error. The array is empty. You can't access elements of an empty array. Point out the exact line causing the error. Use the debugger to step through the code and determine why the array is empty. Commented Feb 17, 2015 at 15:03
  • it throws here: t2 = array[b % a.count]; It returns with 0 objects, so it throws. How do i make it skip over certain parts of code if it has 0 objects Commented Feb 17, 2015 at 15:19
  • 1
    That means that array is empty which means that the a array is empty. Find where you call swap and see why you pass in an empty array. Better yet, fix your swap function so it handles arrays with any number of elements. Commented Feb 17, 2015 at 15:25
  • im using this: raw.githubusercontent.com/samnung/YPlayer/… Commented Feb 17, 2015 at 15:26
  • How do I change the swap function for this? kinda a noobs sorry. Commented Feb 17, 2015 at 15:27

1 Answer 1

3

Because simply if you debug your application, you'll find that in this line

array[1] = t2;

The variable array doesn't have 2 items to access the item #1, it contains either 0 or 1 item, you should do a check like this

if (array.count >= 2) {
    array[1] = t2;
}
Sign up to request clarification or add additional context in comments.

9 Comments

So it brings back a error that there are 0 objects, and it crashes. heres the link to the code im using: raw.githubusercontent.com/samnung/YPlayer/…
Actually I guess the code is good because I can't see any access to the second item of the array, array[1] go and search for it
search for an array? I don't know what to do:( It throws because there is 0 objects, could i make it go to another file if there are 0 objects?
Simply make the check, if (array.count > 0) {
But how do i link it to go to another file?
|

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.