2017-07-14 24 views
2

SwiftでMatrixクラスを作成しようとしていますが、setRow()関数のself.data[row * columns..<(row + 1) * columns] = data行にエラーが表示されています。エラーは 'タイプの値を割り当てることができません' [Double] 'をタイプ' ArraySlice ''Swift:構造体の突然変異アレイ

struct Matrix: CustomStringConvertible { 
    let rows:Int 
    let columns:Int 
    var data:[Double] 

    // Description 
    public var description: String { 
     var description = "" 
     for r in 0..<rows { 
      description += data[r * columns..<(r + 1) * columns].description + "\n" 
     } 
     return description 
    } 

    // Initialisation 
    init(rows: Int, columns: Int) { 
     self.rows = rows 
     self.columns = columns 
     self.data = Array(repeating: 0.0, count: rows * columns) 
    } 

    init(rows: Int, columns: Int, data:[Double]) { 
     assert(data.count == (rows * columns),"Number of elements must equal rows * columns") 
     self.rows = rows 
     self.columns = columns 
     self.data = data 
    } 

    // Validity 
    func validRow(row: Int) -> Bool { 
     return row > 0 && row < rows 
    } 

    func validColumn(column: Int) -> Bool { 
     return column > 0 && column < columns 
    } 

    func validIndex(row: Int, column: Int) -> Bool { 
     return validRow(row: row) && validColumn(column: column) 
    } 

    // Setters and getters 
    func get(row: Int, column: Int) -> Double { 
     assert(validIndex(row: row,column: column), "Index out of range") 
     return data[(row * columns) + column] 
    } 

    mutating func set(row: Int, column: Int, value: Double) { 
     assert(validIndex(row: row,column: column), "Index out of range") 
     data[(row * columns) + column] = value 
    } 

    func getRow(row: Int) -> [Double] { 
     assert(validRow(row: row), "Index out of range") 
     return Array(data[row * columns..<(row + 1) * columns]) 
    } 

    mutating func setRow(row: Int, data:[Double]) { 
     assert(validRow(row: row), "Index out of range") 
     assert(data.count == columns, "Data must be same length ans the number of columns") 
     self.data[row * columns..<(row + 1) * columns] = data 
    } 

    // Swapping 
    mutating func swapRow(row1: Int, row2: Int) { 
     assert(validRow(row: row1) && validRow(row: row2), "Index out of range") 
     let holder = getRow(row: row2) 
     setRow(row: row2, data: getRow(row: row1)) 
     setRow(row: row1, data: holder) 
    } 
} 

答えて

3

エラーが言うように、Array年代はArraySlice、ないArrayで添字取引の範囲でした。

1つの解決策は、@vacawama saysとして、入力配列全体のスライスを作成することです。

mutating func setRow(row: Int, data newData: [Double]) { 
    assert(validRow(row: row), "Index out of range") 
    assert(data.count == columns, "Data must be same length ans the number of columns") 
    data[row * columns ..< (row + 1) * columns] = newData[newData.indices] 
} 

それとも

スウィフト4に、あなたはまったく同じことし the ... 'operator'の利点、取ることができます:

data[row * columns ..< (row + 1) * columns] = newData[...] 

しかし、IMOを、より良いツールこれは、アレイのindicesで添字によって行うことができますそれはあなたが新しいelemは、任意のCollectionに対処することを可能にするよう

mutating func replaceSubrange<C>(_ subrange: Range<Int>, with newElements: C) 
    where Element == C.Element, C : Collection 

:仕事のためにここにreplaceSubrange(_:with:)だろうあなたもsetRow汎用作ることができることを意味エント、:

mutating func setRow<C : Collection>(row: Int, data newData: C) 
    where C.Iterator.Element == Double { // <- in Swift 4, remove ".Iterator" 

    assert(validRow(row: row), "Index out of range") 
    assert(data.count == columns, "Data must be same length ans the number of columns") 
    data.replaceSubrange(row * columns ..< (row + 1) * columns, with: newData) 
} 
+0

内の値の配列を設定したいですか? 'var a = [1、2、3、4、5]; a [1 .. <3] = [6,7] 'となる。 – vacawama

+1

@vacawama 'ArraySlice'は' ExpressibleByArrayLiteral'ですので、配列リテラル '[6,7]'は 'ArraySlice 'と推測されます。 – Hamish

+1

ありがとうございます。元気回復! ;-) – vacawama

0

簡単なサウンドです。あなたの機能に[Double]の代わりにDoubleだけを設定したかったのですか?

mutating func setRow(row: Int, data: Double) { 
     assert(validRow(row: row), "Index out of range") 
     assert(data.count == columns, "Data must be same length ans the number of columns") 
     self.data[row * columns..<(row + 1) * columns] = data 
    } 
+0

いいえ、私はこの仕事をするのはなぜ 'self.data'配列 – BenJacob