2016-06-10 69 views
1

我試圖建立一個FUNC允許總結兩個MATRIX如果他們是等於相同的尺寸,但我得到的嘗試錯誤「EXC_BAD_INSTRUCTION」 ..斯威夫特矩陣和

人有一個想法?我完全封鎖在這個時候..

這裏是我的遊樂場:

import UIKit 

enum RisedError: ErrorType { 
    case DimensionNotEquals 
    case Obvious(String) 
} 

func ==(lhs: Matrix, rhs: Matrix) -> Bool { 
    return (lhs.rows) == (rhs.rows) && (lhs.columns) == (rhs.columns) 
} 

protocol Operation { 
    mutating func sumWith(matrixB: Matrix) throws -> Matrix 
} 

struct Matrix { 
    let rows: Int, columns: Int 
    var grid: [Double] 
    init(rows: Int, columns: Int) { 
     self.rows = rows 
     self.columns = columns 
     grid = Array(count: rows * columns, repeatedValue: 0.0) 
    } 
    func indexIsValidForRow(row: Int, column: Int) -> Bool { 
     return row >= 0 && row < rows && column >= 0 && column < columns 
    } 
    subscript(row: Int, column: Int) -> Double { 
     get { 
      assert(indexIsValidForRow(row, column: column), "Index out of range") 
      return grid[(row * columns) + column] 
     } 
     set { 
      assert(indexIsValidForRow(row, column: column), "Index out of range") 
      grid[(row * columns) + column] = newValue 
     } 
    } 
} 

var matrixA = Matrix(rows: 2, columns: 2) 
matrixA[0,0] = 1.0 
matrixA[0,1] = 2.0 
matrixA[1,0] = 3.0 
matrixA[1,1] = 4.0 
var matrixB = Matrix(rows: 2, columns: 2) 
matrixB[0,0] = 5.0 
matrixB[0,1] = 6.0 
matrixB[1,0] = 7.0 
matrixB[1,1] = 8.0 

print(matrixA) 
print(matrixB) 


extension Matrix: Operation { 

    mutating func sumWith(matrixB: Matrix) throws -> Matrix { 

     guard self == matrixB else { throw RisedError.DimensionNotEquals } 

     for row in 0...self.rows { 
      for column in 0...self.columns { 
       self[row, column] = matrixB[row, column] + self[row, column] 
      } 
     } 
     return self 
    } 

} 

do { 
    try matrixA.sumWith(matrixB) 
} catch RisedError.DimensionNotEquals { 
    print("The two matrix's dimensions aren't equals") 
} catch { 
    print("Something very bad happens") 
} 

以下是錯誤日誌:

enter image description here

+0

你能顯示其餘的錯誤日誌嗎? – Fogmeister

+4

如果只是矩陣*尺寸*是相同的,你真的定義'A == B'爲真嗎? –

+0

如果在Playground中發生意外事件:1)使用編譯的項目,2)使用* debugger *單步執行代碼。在你的2x2矩陣的情況下,問題幾乎會立即顯現! –

回答

1

的問題是你使用的closed range operator在你的for循環0...self.rows。這將包括迭代中範圍的上限,在你的情況下,它的範圍是越界並因此會崩潰。

您要使用的半開區間操作..<代替:

for row in 0..<self.rows { 
    for column in 0..<self.columns { 
     self[row, column] = matrixB[row, column] + self[row, column] 
    } 
} 

這將重複直到但不包括上限。


我還要指出@MartinR's comment above - 定義矩陣平等僅基於尺寸是相同的,似乎不合邏輯。請記住,平等意味着可替代性(即,如果a == b,ab是可互換的)。

我會考慮改變你的==檢查兩個維度值,然後在你的sumWith方法實現自己的尺寸檢查(或創建一個新的方法來比較的尺寸)。

1

其實你的錯誤是指數超出範圍

替換這個代碼的擴展。

extension Matrix: Operation { 

    mutating func sumWith(matrixB: Matrix) throws -> Matrix { 

     guard self == matrixB else { throw RisedError.DimensionNotEquals } 

     for row in 0...self.rows - 1 { 
      for column in 0...self.columns - 1 { 
       self[row, column] = matrixB[row, column] + self[row, column] 
      } 
     } 
     return self 
    } 
} 

希望它能解決您的問題。

+0

我的恥辱..你解決了我的問題:) –

+1

這是我的感受:) – Bhumi