所以我試圖通過查詢將信息上傳到Firebase,並且我陷入了一些這種邏輯。我的問題是,當我嘗試將我創建的對象存儲到一個數組中時,出現一個錯誤,表示我無法使用[any]來標記[[any]]。現在我正在努力完成的事情,並不是有一堆if-else語句,而是使用for-loop來運行我應該將對象放入哪個數組中的數據。我只是有點卡在我應該如何正確追加它,並會很樂意協助!使用數組存儲數組 - Swift
我的錯誤是"Cannot subscript a value of type [Any] with an index of type Any"
這裏是我的數據結構:
/** Setting up the Data **/
var genresLabelIndex = ["Fiction", "Poetry", "NonFiction", "Science"]
var fiction = [InformationForFeed]()
var poetry = [InformationForFeed]()
var nonFiction = [InformationForFeed]()
var science = [InformationForFeed]()
var allGenresData = [[Any]]()
func getData() {
for genre in genresLabelIndex {
let dbReference = FIRDatabase.database().reference().child("genres").child(genre)
let query = dbReference.queryOrderedByKey().queryLimited(toFirst: 3)
query.observeSingleEvent(of: .value, with: { (snapshot : FIRDataSnapshot) in
if snapshot.childrenCount > 0 {
for snap in snapshot.children.allObjects as! [FIRDataSnapshot] {
let item = snap.value as! Dictionary<String,AnyObject?>
let data = InformationForFeed(dictionary: item as Dictionary<String,AnyObject>)
for i in (self.allGenresData) {
self.allGenresData[i].append(data)
}
}
} else {
print("This snap shot doesnt have any data")
}
})
}
print(allGenresData)
}
我的錯誤是在行self.allGenresData[i].append(data)
在我的init()我做了以下內容:
allGenresData = [self.fiction, self.poetry, self.nonFiction, self.science]
getData()
你會得到什麼樣的錯誤? – paper1111
「不能用任何類型的下標索引類型[Any]的值」 – AndrewS