First about me
University student, research on sound, programming in swift.

Declaration of initialization of two-dimensional array and calculation of array elements in swift
Currently, it is implemented by handling 2D arrays in swift. I thought that it would be more convenient to initialize the array first as I implemented it, so I would like to help you with the two-dimensional array initialization declaration. In addition, since it is necessary to calculate the elements of the array, I would like to seek advice on the calculation of the arrays.

Issued problem (1) -2D array initialization declaration

What is the two-dimensional array initialization declaration?

var buffer: [[Float]] = [[Float]] (repeating: [Int] (repeating: 0.0, count: nframe), count: 1)

* Nframe contains a value of 6000.
When initialized like this, I tried to implement it because I saw it on other webs
Type of expression is ambiguous without more context and error display.
The content was ambiguous and angry

Issued problem ②-Calculation between sequences
let nframe = 6000
var buffer: [[Float]] = Array >()
var buffer2: [Float]! = Array (repeating: 0.0, count: buffer2size)
var y: [Float] = Array (repeating: 0.0, count: nframe)
for i in 0 ..<nChannel {
     for n in 0 ..<nframe {
        for k in 0 ..<buffer2size {
            y [n] + = buffer [i] [n-k] * buffer2 [k]

nChannel is 0 (i.e. 1Channel), nframe is 6000, buffer2size is 600.
There are 6000 values ​​in the buffer. buffer2 contains 600 values.
What I want to do is multiply-add operation.
This is an attempt to multiply the values ​​stored in buffer and buffer2 above and put the sum of the products looped up to 600 times in y [n].

However, the following error occurs:

error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code = EXC_I386_INVOP, subcode = 0x0).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.

Thank you.

Supplemental information (FW/tool version etc.)

swift5, xcode13.2

  • Answer # 1

    Only for the time being.


    var buffer: [[Float]] = [[Float]] (repeating: [Int] (repeating: 0.0, count: nframe), count: 1)

    Changing [Int] in the middle to [Float] should compile.

  • Answer # 2

    Premises for using arrays
    Check if the stored array contains data properly (you can see the contents by using print debugging or playground), and writing code that exceeds the array index will result in an index out of range. It is careful about how to use.