如何在Swift iOS中从Dictionary保存和检索Array of Objects值?

时间:2022-04-10 13:02:37

I am newly to Swift programming. I am developing an app where I have an array of Objects that I need to save it into one Dictionary, means for Dictionary 'Array of Objects' should be my Value and 'ID' should be my Key.

我刚接触Swift编程。我正在开发一个应用程序,我有一个对象数组,我需要将它保存到一个字典中,意味着字典'对象数组'应该是我的值,'ID'应该是我的密钥。

如何在Swift iOS中从Dictionary保存和检索Array of Objects值?

Here 2 elements contains in my Array, how can I save and retrieve from Dictionary using any unique Key. Please suggest me. Thank you!

这里有2个元素包含在我的数组中,如何使用任何唯一的键从Dictionary保存和检索。请建议我。谢谢!

3 个解决方案

#1


0  

You can do it by

你可以做到

var myDict = Dictionary<String, Any>()
myDict = ["myKey": myArrayObjectName as! VideoRangeInfo]

Now you can access you array by use "myKey" of dictionary like below,

现在您可以使用下面的字典“myKey”访问您的数组,

print("My array = \(myDict["myKey"] as! VideoRangeInfo)")

UPDATED: I think your array is objective c NSMutableArray not swift Array so

更新:我认为你的数组是客观的c NSMutableArray不是swift数组所以

var myDic = NSMutableDictionary()
myDic.setObject(myArrayObjectName, forKey: "myKey")

And

print("My array = \(myDic.object(forKey: "MyKey") as! VideoRangeInfo)")

#2


0  

One simple approach is:

一个简单的方法是:

struct VideoRangeInfo {
    var name: String
}

var arrayVideoRange = [
    VideoRangeInfo(name: "Name1"),
    VideoRangeInfo(name: "Name2"),
    VideoRangeInfo(name: "Name3")
]

var dictionary = [String: VideoRangeInfo]()
for obj in arrayVideoRange {
    dictionary.updateValue(obj, forKey: obj.name)
}

print(dictionary["Name1"]!)
print(dictionary["Name2"]!)
print(dictionary["Name3"]!)

#3


0  

Saving var dictionary = Int:String

保存var dictionary = Int:String

dictionary.updateValue(value: self.arrangeVideoInfo[0]!, forKey: 1)
dictionary.updateValue(value: self.arrangeVideoInfo[0]!, forKey: 2)

Getting

let value1 = dictionary[1] as! VideoRangeInfo
let value2 = dictionary[2] as! VideoRangeInfo

#1


0  

You can do it by

你可以做到

var myDict = Dictionary<String, Any>()
myDict = ["myKey": myArrayObjectName as! VideoRangeInfo]

Now you can access you array by use "myKey" of dictionary like below,

现在您可以使用下面的字典“myKey”访问您的数组,

print("My array = \(myDict["myKey"] as! VideoRangeInfo)")

UPDATED: I think your array is objective c NSMutableArray not swift Array so

更新:我认为你的数组是客观的c NSMutableArray不是swift数组所以

var myDic = NSMutableDictionary()
myDic.setObject(myArrayObjectName, forKey: "myKey")

And

print("My array = \(myDic.object(forKey: "MyKey") as! VideoRangeInfo)")

#2


0  

One simple approach is:

一个简单的方法是:

struct VideoRangeInfo {
    var name: String
}

var arrayVideoRange = [
    VideoRangeInfo(name: "Name1"),
    VideoRangeInfo(name: "Name2"),
    VideoRangeInfo(name: "Name3")
]

var dictionary = [String: VideoRangeInfo]()
for obj in arrayVideoRange {
    dictionary.updateValue(obj, forKey: obj.name)
}

print(dictionary["Name1"]!)
print(dictionary["Name2"]!)
print(dictionary["Name3"]!)

#3


0  

Saving var dictionary = Int:String

保存var dictionary = Int:String

dictionary.updateValue(value: self.arrangeVideoInfo[0]!, forKey: 1)
dictionary.updateValue(value: self.arrangeVideoInfo[0]!, forKey: 2)

Getting

let value1 = dictionary[1] as! VideoRangeInfo
let value2 = dictionary[2] as! VideoRangeInfo