2016-09-27 64 views
0
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 

     } 
+0

'返回[ 「名」:標題, 「addedByUser」:字幕, 「完成」:IMAGEURL ]作爲AnyObject' –

回答

1

Dictionary是一個迅速值。而AnyObject只接受參考類型

所以爲了返回Dictionary使用Any而不是AnyObject

func toAnyObject() -> Any { 
     return [ 
      "name": title, 
      "addedByUser": subtitle, 
      "completed": imageURL 
     ] 
    } 
+0

我更新了題。 –

+0

categoryRef是什麼類型? – PGDev

0

它轉換爲NSDictionary

func toAnyObject() -> AnyObject { 
    return [ 
     "name": title, 
     "addedByUser": subtitle, 
     "completed": imageURL 
    ] as NSDictionary 
}