2016-09-27 44 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 
}