簡體   English   中英

如何在Swift 4中聲明數組索引的類型

[英]How do I declare the type of an array index in swift 4

我正在嘗試創建一個2D查找表,其中的枚舉表示swift 4中表格的軸,例如:

enum ECT: Int {
  case cool = 0
  case normal
  case above_range
}

enum Load: Int {
  case idle = 0
  case cruise
  case wot
}

var timingRetard = [[Double?]](repeating: [nil,nil,nil], count 3)

(不,我不是在Swift中編寫嵌入式PCM代碼(那會很有趣!),但這是我嘗試使用的swift構造的一個簡單示例,但是還沒有弄清楚語法)

如何使用枚舉作為索引來分配和檢索數組中的值,例如假設元素已經創建

timingRetard[cool][idle] = 0.0
timingRetard[cool][cruise] = 0.0
timingRetard[cool][wot] = 0.0
timingRetard[above_range][wot] = -8.0
timingRetard[normal][cruise] = 0.0

如何在給定每個軸枚舉數的情況下初始化數組后,如何聲明Swift數組的索引類型只能由枚舉類型訪問? 我以為可以在一個結構中添加TimingRetard並聲明一個下標方法來將索引限制為枚舉類型,但也沒有做到這一點。

一種實現的方法是在結構中覆蓋下標方法。 這里解釋得更好。 我使用鏈接中提到的示例以這種方式解決您的問題:

enum ECT: Int{
    case cool = 0
    case normal
    case above_range
}

enum Load: Int {
    case idle = 0
    case cruise
    case wot
}

struct TimingRetard2D {
    let rows: Int, cols: Int
    private(set) var array:[Double?]

    init(rows: Int, cols: Int, repeating:Double?) {
        self.rows = rows
        self.cols = cols
        array = Array(repeating: repeating, count: rows * cols)
    }

    private func indexIsValid(row: Int, col: Int) -> Bool {
        return row >= 0 && row < rows && col >= 0 && col < cols
    }

    subscript(row: ECT, col: Load) -> Double? {
        get {
            assert(indexIsValid(row: row.rawValue, col: col.rawValue), "Index out of range")
            return array[(row.rawValue * cols) + col.rawValue]
        }
        set {
            assert(indexIsValid(row: row.rawValue, col: col.rawValue), "Index out of range")
            array[(row.rawValue * cols) + col.rawValue] = newValue
        }
    }
}

var timingRetard = TimingRetard2D.init(rows: 3, cols: 3, repeating: nil)
timingRetard[.cool, .idle] = 0.0
timingRetard[.cool, .cruise] = 0.0
timingRetard[.cool, .wot] = 0.0
timingRetard[.above_range, .wot] = -8.0
timingRetard[.normal, .cruise] = 0.0
print(timingRetard.array)

OUTPUT:

[可選(0.0),可選(0.0),可選(0.0),無,可選(0.0),無,無,無,可選(-8.0)]

對Puneet解決方案的次要更新,該解決方案允許使用數組數組而不是將偏移量計算為一維數組:

enum ECT: Int{
    case cool = 0
    case normal
    case above_range
}

enum Load: Int {
    case idle = 0
    case cruise
    case wot
}

struct TimingRetard2D {
    let rows: Int, cols: Int
    private(set) var array:[[Double?]]

    init(rows: Int, cols: Int, repeating:Double?) {
        self.rows = rows
        self.cols = cols
        let initRow = Array(repeating: repeating, count: cols)
        array = Array(repeating: initRow, count: rows)
    }

    private func indexIsValid(row: Int, col: Int) -> Bool {
        return row >= 0 && row < rows && col >= 0 && col < cols
    }

    subscript(row: ECT, col: Load) -> Double? {
        get {
            assert(indexIsValid(row: row.rawValue, col: col.rawValue), "Index out of range")
            return array[row.rawValue][col.rawValue]
        }
        set {
            assert(indexIsValid(row: row.rawValue, col: col.rawValue), "Index out of range")
            array[row.rawValue][col.rawValue] = newValue
        }
    }
}

var timingRetard = TimingRetard2D.init(rows: 3, cols: 3, repeating: nil)
timingRetard[.cool, .idle] = 0.0
timingRetard[.cool, .cruise] = 0.0
timingRetard[.cool, .wot] = 0.0
timingRetard[.above_range, .wot] = -8.0
timingRetard[.normal, .cruise] = 0.0
print(timingRetard.array)

但是,還有沒有其他方法可以使結構的使用者也使用2D數組語法? 例如TimingRetards [.cool] [。wot]

您可以這樣寫:

extension Array where Element == Double? {
    subscript(load: Load) -> Element {
        get {
            return self[load.rawValue]
        }
        set {
            self[load.rawValue] = newValue
        }
    }
}

extension Array where Element == [Double?] {
    subscript(ect: ECT) -> Element {
        get {
            return self[ect.rawValue]
        }
        set {
            self[ect.rawValue] = newValue
        }
    }
}


var timingRetard = [[Double?]](repeating: [nil,nil,nil], count: 3)

timingRetard[.cool][.idle] = 0.0
timingRetard[.cool][.cruise] = 0.0
timingRetard[.cool][.wot] = 0.0
timingRetard[.above_range][.wot] = -8.0
timingRetard[.normal][.cruise] = 0.0

但是,最好擴展TimingRetard2D

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM