func toAnyObject() -> AnyObject {
return [
"name": title,
"addedByUser": subtitle,
"completed": imageURL
]
}
以上代碼生成以下錯誤:返回夫特字典作爲AnyObject
Contextual type 'AnyObject' cannot be used with dictionary literal
更新:我想創建的toAnyObject功能,並利用它在火力地堡應用。我得到如下:
, reason: '(setValue:) Cannot store object of type _SwiftValue at name. Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray.'
let rootRef = FIRDatabase.database().reference()
let categoriesRef = rootRef.child("categories")
let annuals = FlowerCategory(id: 1, title: "Annuals",subtitle :"Plants that just last one season. These plants will die in the winter. ", imageURL: "annuals.jpg")
let perennials = FlowerCategory(id: 2, title: "Perennials",subtitle :"Plants come back year after year. These are also very less expensive", imageURL: "Perennial.jpg")
let flowerCategories = [annuals,perennials]
for flowerCategory in flowerCategories {
let categoryRef = categoriesRef.child("category")
categoryRef.setValue(flowerCategory.toAnyObject()) <- THIS LINE ERROR
}
'返回[ 「名」:標題, 「addedByUser」:字幕, 「完成」:IMAGEURL ]作爲AnyObject' –