简体   繁体   English

将Swift字典作为AnyObject返回

[英]Returning Swift Dictionary as AnyObject

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

The above code produces the following error: 上面的代码产生以下错误:

Contextual type 'AnyObject' cannot be used with dictionary literal

UPDATE: I am trying to create the toAnyObject function and utilize it in a Firebase application. 更新:我正在尝试创建toAnyObject函数并在Firebase应用程序中使用它。 I get the following: 我得到以下内容:

, 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

        }

Dictionary is a value type in swift. Dictionary是一种快速的值类型 And AnyObject only accepts reference types . 而且AnyObject只接受引用类型

So in order to return a Dictionary use Any instead of AnyObject . 因此,为了返回Dictionary使用Any而不是AnyObject

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

Cast it to NSDictionary : 投射到NSDictionary

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift-[NSObject:AnyObject]! 不是“字典”的子类型 <String, AnyObject> - Swift - [NSObject : AnyObject]!' is not a subtype of 'Dictionary<String, AnyObject> Swift-使用&#39;as!时为零! 字典 <String, AnyObject> &#39; - Swift - Nil when using 'as! Dictionary<String, AnyObject>' Swift 3-排序数组/字典:&#39;(AnyObject)&#39;不是&#39;NSString&#39;的子类型 - Swift 3 - Sorting array/dictionary: '(AnyObject)' is not a subtype of 'NSString' Swift 4字典[String:AnyObject]按字母顺序按键排序 - Swift 4 Dictionary [String : AnyObject] sort by key in alphabetical order 如何快速使用过滤器功能删除数组的字典[String:AnyObject]? - How to remove dictionary[String:AnyObject] for array using filter function in swift? Swift Firebase - 上下文类型“AnyObject”不能与字典文字一起使用 - Swift Firebase - Contextual type 'AnyObject' cannot be used with dictionary literal swift的最新版本:[String,String?]不能转换为字典<String, AnyObject> - Latest version of swift: [String, String?] is not convertible to Dictionary<String, AnyObject> 更新到Swift 3.0:上下文类型“AnyObject”不能与字典文字一起使用 - Updating to Swift 3.0: Contextual type 'AnyObject' cannot be used with dictionary literal Swift,Json字典 <String, AnyObject> 向标签发送值 - Swift, Json Dictionary<String, AnyObject> send value to label Swift 3-错误“上下文类型“ AnyObject”无法与字典文字一起使用” - Swift 3 - Error 'Contextual type 'AnyObject' cannot be used with dictionary literal'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM