I am getting the "Thread 1: Fatal error: Index out of range" in the line where I try to append some value to an array

I am getting a runtime error when I hit the button.

I really hope you have an answer for me


Here is my code



@IBActionfunc Calculate2(_ sender: Any) {
       
        // |Z| - O QUE FALTA FAZER
        var xiNor1 = [Double]()
        var xiNor2 = [Double]()
        var zi1 = [Double]()
        var zi2 = [Double]()
        var x2 = 0.0
        var y2 = 0.0
        let Nn = N.integerValue
        let Mm = M.integerValue
        let sigmaN = Б.doubleValue
        let a = A.doubleValue
        let b = B.doubleValue
        var z = 0.0
        let Ua = 1.96

        for j in 0..            for l in 0..<12 {
                xiNor1.insert( Double(l), at: j)
            }
            xiNor1[j] -= 6;
            zi1[j] = sigmaN * xiNor1[j] + a; // Thread 1: Fatal error: Index out of range
        }
       
        for j in 0..            x2 += zi1[j]
        }
        x2 /= Double(Nn)
       
        for j in 0..            for l in 0..<12 {
                xiNor2.insert( Double(l), at: j)
            }
            xiNor2[j] -= 6;
            zi2[j] = sigmaN * xiNor2[j] + b;
        }
       
        for j in 0..            y2 += zi2[j]
        }
        y2 /= Double(Mm)
       
        z = abs( (x2 - y2) / sqrt( (pow(Double(sigmaN), 2) / Double(Nn)) + (pow(Double(sigmaN), 2) / Double(Mm)) ) )
       
        if (z > Ua) {
            Z.doubleValue = z
            res.stringValue = "H1: mx ≠ my "
        } else {
            Z.doubleValue = z
            res.stringValue = "H0: mx = my"
        }
    }
Answered by DTS Engineer in 354680022

An Index out of range error means that your program has accessed an array by index and the supplied index is not valid for that array. The line in question:

zi1[j] = sigmaN * xiNor1[j] + a;

has two indexed accesses, one for

zi1
and another for
xiNor1
. I recommend that you split these up to see what one is the problem:
let tmp = sigmaN * xiNor1[j] + a;
zi1[j] = tmp

I suspect it’ll be the second one,

zi1
. Looking back through your code I see you allocate an empty array:
var zi1 = [Double]()

and then never add any elements to it. If that’s the case, and it’s not just that your code has been munged as you posted it, then no value of

j
will work.

If you want to append a value to an array, use the

append(_:)
method. For example:
var a = [Int]()
a.append(42)

Alternatively, if you know how big the array should be in advance, you can preallocate all of your elements:

var a = [Int](repeating: 0, count: 16)

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Accepted Answer

An Index out of range error means that your program has accessed an array by index and the supplied index is not valid for that array. The line in question:

zi1[j] = sigmaN * xiNor1[j] + a;

has two indexed accesses, one for

zi1
and another for
xiNor1
. I recommend that you split these up to see what one is the problem:
let tmp = sigmaN * xiNor1[j] + a;
zi1[j] = tmp

I suspect it’ll be the second one,

zi1
. Looking back through your code I see you allocate an empty array:
var zi1 = [Double]()

and then never add any elements to it. If that’s the case, and it’s not just that your code has been munged as you posted it, then no value of

j
will work.

If you want to append a value to an array, use the

append(_:)
method. For example:
var a = [Int]()
a.append(42)

Alternatively, if you know how big the array should be in advance, you can preallocate all of your elements:

var a = [Int](repeating: 0, count: 16)

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

It really helped to use the "append(_: )" method instead of just assigning the value to the array "zi1"

Thank you very much for your help!

I really appreciate it!

I am getting the "Thread 1: Fatal error: Index out of range" in the line where I try to append some value to an array
 
 
Q