简体   繁体   English

尝试保存数组时出现错误-无法将类型[Data]的值转换为预期的参数类型&#39;[Dictionary <String, AnyObject> ]”

[英]Trying to saveArray, got error - Cannot convert value of type '[Data]' to expected argument type '[Dictionary<String, AnyObject>]'

I'm trying to save array added from tableview using this function: 我正在尝试使用此功能保存从tableview添加的数组:

class func saveArray(_ value: [Dictionary<String, AnyObject>], key: String) {
        let data = NSKeyedArchiver.archivedData(withRootObject: value)
        UserDefaults.standard.set(data, forKey: key)
    }

Below is the function where I want to save the array: 下面是我要保存数组的函数:

func addItemCat(items: [Data]) {
        print("ITEM: ", items)
        dataSource.myListTableViewController.myListArr = items
        self.myListTV.isHidden = false
        UserDefaultsHelper.saveArray(items, key: Constants.myList.myList)
    }

However, I got this error: Cannot convert value of type '[Data]' to expected argument type '[Dictionary String, AnyObject ]' 但是,我收到此错误:无法将类型[Data]的值转换为预期的参数类型'[Dictionary String,AnyObject]'

Below is my Data model: data model screencap 以下是我的数据模型: 数据模型屏幕截图

I'm new to Swift and I hope someone can explain what is the problem. 我是Swift的新手,我希望有人可以解释问题所在。

Problem is with data types, saveArray function expects value parameter of type array of dictionary [Dictionary<String, AnyObject>] , but you are passing array of data model objects which is a type-mismatch error. 问题在于数据类型, saveArray函数需要字典[Dictionary<String, AnyObject>]的类型数组的value参数,但是您正在传递数据模型对象的数组,这是类型不匹配的错误。

To solve this: 要解决这个问题:

First, You should not use pre-defined keywords for creating your custom object. 首先,您不应使用预定义的关键字来创建您的自定义对象。 Use DataObject instead: 改用DataObject

struct DataObject {

}

Now change your saveArray function as: 现在将您的saveArray函数更改为:

class func saveArray(_ value: [DataObject], key: String) {
    let data = NSKeyedArchiver.archivedData(withRootObject: value)
    UserDefaults.standard.set(data, forKey: key)
}

and addItemCat , function as: addItemCat ,功能如下:

func addItemCat(items: [DataObject]) {
    print("ITEM: ", items)
    dataSource.myListTableViewController.myListArr = items
    self.myListTV.isHidden = false
    UserDefaultsHelper.saveArray(items, key: Constants.myList.myList)
}

暂无
暂无

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

相关问题 无法转换类型[[String:AnyObject] ?. Type”(也称为“可选” <Dictionary<String, AnyObject> &gt; .Type)到期望的参数类型 - Cannot convert value of type '[String : AnyObject]?.Type' (aka 'Optional<Dictionary<String, AnyObject>>.Type) to expected argument type 无法将“NSMutableDictionary”类型的值转换为预期的参数类型“[String: AnyObject]?” - Cannot convert value of type 'NSMutableDictionary' to expected argument type '[String: AnyObject]?' 无法将“String”类型的值转换为预期的参数类型“AnyObject?” - Cannot convert value of type 'String' to expected argument type 'AnyObject?' 无法将类型“ [AnyObject]”的值转换为预期的参数类型“ [MKAnnotation]”错误 - Cannot convert value of type '[AnyObject]' to expected argument type '[MKAnnotation]' error 无法将类型'(key:String,value:AnyObject)'的值转换为预期的参数类型'[String:AnyObject]' - Cannot convert value of type '(key: String, value: AnyObject)' to expected argument type '[String : AnyObject]' 无法转换NSMutableDictionary类型的值?预期的参数类型[NSObject:AnyObject]! - Cannot convert value of type NSMutableDictionary? to expected argument type [NSObject: AnyObject]! 无法将类型“ [AnyObject]”的值转换为预期的参数类型“ [UIViewController]?” - Cannot convert value of type'[AnyObject]'to expected argument type '[UIViewController]?' Swift类别:“无法将类型的值转换为预期的参数类型&#39;AnyObject!&#39; - Swift category: "Cannot convert value of type to expected argument type 'AnyObject!' 无法转换类型为“任何对象!”的值 到预期的参数类型“ Int” - cannot convert value of type 'Anyobject!' to expected argument type 'Int' 无法将&#39;NSObject - &gt;() - &gt; PostFeed&#39;类型的值转换为预期的参数类型&#39;AnyObject?&#39; - Cannot convert value of type 'NSObject -> () -> PostFeed' to expected argument type 'AnyObject?'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM