2016-03-17 18 views
0

爲什麼不能正常工作?使用類作爲鍵的字典數組索引超出範圍 - swift

我試圖將字典的值賦給false的錯誤是它失敗的地方。它返回此錯誤「致命錯誤:數組索引超出範圍」,如底部輸出中所示。

var tupleCount = 0 
for var i = 0; i < width; ++i { 
    for var j = 0; j < height; ++j { 

     arrayOfTupleClass.append(TupleClass(newX: i, newY: j, newXMax: width, newYMax: height)) 

     print("arrayOfTupleClass.count: \(arrayOfTupleClass.count)") 
     print("arrayOfTupleClass[tupleCount]: \(arrayOfTupleClass[tupleCount])") 
     print("tupleCount: \(tupleCount)") 
     print("imageNum: \(imageNum)") 

     // placing '0' in place of dictionary Array index for simplicity 
     pointDictionaryArray[0][arrayOfTupleClass[tupleCount]] = false // <-- error here 

     tupleCount++ 
    } 
} 

這是怎麼我的字典陣列設置:

var arrayOfTupleClass = [TupleClass]() 
var pointDictionaryArray = [[TupleClass: Bool]]() 

這是我的TupleClass其中應包括對具有類作爲字典的關鍵,因爲我做了哈希的。

class TupleClass: Hashable { 
    var x: Int! 
    var y: Int! 
    let yMax: Int! 
    let xMax: Int! 

    var weight: Int = 0 

    init(newX: Int, newY: Int, newXMax: Int, newYMax: Int) { 
     x = newX 
     y = newY 
     yMax = newYMax 
     xMax = newXMax 
    } 

    func setWeight(newWeight: Int) { 
     weight = newWeight 
    } 

    func getWeight() -> Int { 
     return weight 
    } 

    // required for the Hashable protocol 
    var hashValue: Int { 
     return x * yMax + y 
    } 
}; 

// required function for the Equatable protocol, which Hashable inheirits from 
func ==(left: TupleClass, right: TupleClass) -> Bool { 
    return (left.x == right.x) && (left.y == right.y) 
} 

這是輸出:

arrayOfTupleClass.count: 1 
arrayOfTupleClass[tupleCount]: My_Project_Name.TupleClass 
tupleCount: 0 
imageNum: 0 
fatal error: Array index out of range 
+0

我在錯誤行上用'0'替換的變量是'imageNum'btw,這不是問題,但我覺得有人會去把它提起來,所以我現在提起它 – brw59

+0

你可以嘗試.. pointDictionaryArray [0] [arrayOfTupleClass [tupleCount] -1] = false。數組數總是比數組最後一個元素的索引高1。例如,一個由10個元素構成的數組索引爲0-9,所以試圖引用元素「10」會引發錯誤。我打算將此作爲答案發布,但我不像其他人那樣熟悉這種語言。 – LuvnJesus

+0

@LuvnJesus「arrayOfTupleClass [tupleCount]」返回一個類實例,所以我不能從中減去一個數字。我試了它,並確認它不起作用 – brw59

回答

0

隨着AR我知道你不能使用SWIFT類的名稱作爲參考(也不是一個對象 - 一個在更新的版本)。然而,爲什麼現在參考

let myclassName = self.className 
    let anotherClassName = NSClassFromString(self.classForCoder) 

我只是拼湊一個快速操場例如玩弄:

import Foundation 

debugPrint("Hello, World, Here we go from the PLayground template!") 

let width = 100 // from your code 
let height = 100 // from yoru code 

typealias theTup = (newX: CGFloat, newY: CGFloat, newXMax: CGFloat,  newYMax: CGFloat) // setting a typ alias to your tuple format 


var theTupArray: [theTup] /*Strong typing*/ = [theTup]() // lets make a new array (with default zero element value) 

// lets add some default values for testing, nothing special 
theTupArray.appendContentsOf([ 
    (newX: 1.0, newY: 2.0, newXMax: 2.0, newYMax: 5.0), 
    (newX: 2.0, newY: 3.0, newXMax: 4.0, newYMax: 10.0), 
    (newX: 3.0, newY: 4.0, newXMax: 6.0, newYMax: 15.0), 
    ] 
) 

// now for-each element in the `theTupArray`... See Swift .forEach.. Or other shorthand methods like .map etc... 

theTupArray.forEach { (
    newX: CGFloat, 
    newY: CGFloat, 
    newXMax: CGFloat, 
    newYMax: CGFloat) in 

    debugPrint("NewX 1:\(newX), NewY: \(newY), NewXMax: \(newXMax), NewYMax: \(newYMax)", terminator: "\n") 

} 

希望這個快速重寫幫助:)

+0

我知道我可以使用類或結構作爲參考,我只是還沒有成功地做到這一點:http://stackoverflow.com/questions/31438210/how-爲了實現可哈希協議在快速的一個整型數組的一個自定義條件 – brw59

+0

只是一個鏡頭,你嘗試過:'pointDictionaryArray [0] .valueForKey'?或者通過在事物的Obj-C方面使用「valueForKey」來引用字典嗎? –

+0

只是添加,不適用於不是從Obj-C類派生的預先快速「類」... –