问题描述
[我是Swift的新手,我不知道这是否可能,所以请建议我]
[I am new to Swift, I don't know is this possible or not, so please suggest me]
我有一个这样的字典(动态的):
I have a dictionary (which is dynamic) like this:
let simpleHash = ["testA": "A", "testB": "B", "testC": "C"]
我想将其转换为对象,以便可以像这样访问:
I want to convert this to an Object, so that I can access like:
simpleHash.testA // instead of simpleHash["testA"]
我尝试了以下方法,但没有帮助
I have tried the below one, but it didn't help
let jsonData = try JSONSerialization.data(withJSONObject: simpleHash, options: .prettyPrinted)
let decoded = try JSONSerialization.jsonObject(with: jsonData, options: [])
有人可以在这方面建议我吗?预先感谢!
Can anyone please suggest me on this.Thanks in advance!
推荐答案
Swift需要为testA明确声明一个变量,因此您将无法100%动态.但是,由于您需要在代码中使用该变量,因此在某些时候它是已知的.鉴于此,本着最小化声明约束的精神,您可以定义一个类,该类使用字典作为其内部存储,并将键值作为计算的属性公开.
Swift will need an explicitly declared variable for testA so you will not be able to be 100% dynamic. But, since you need to use the variable in code, it will be known at some point. Given this and in the spirit of minimizing the declaration constraints, you could define a class that uses the dictionary as its internal storage and exposes the key values as computed properties.
这是一个例子:
class DictionaryBased
{
var content:[String:Any]
init(_ dictionary:[String:Any])
{ content = dictionary }
func get<T>(_ key:String, _ defaultValue:T) -> T
{ return content[key] as? T ?? defaultValue }
func set<T>(_ key:String, _ value:T)
{ content[key] = value }
}
class SimpleHash:DictionaryBased
{}
使用此功能,您可以根据需要使用扩展名添加计算的属性.
With this, you can add computed properties as needed (and where needed) using extensions.
extension SimpleHash
{
var testA:String { get { return get("testA", "") } set { set("testA",newValue) } }
var testB:String { get { return get("testB", "") } set { set("testB",newValue) } }
// if variables are "read-only", you don't need the set { } part
var testC:String { get { return get("testC", "") } }
}
您可以添加键入或不键入并支持可选变量的变量,或者(如上所述)提供默认值.
You can add variables that are typed or not and support optionals or, (as above) provide default values.
extension SimpleHash
{
var testD:Any? { get { return get("testD", nil) } set { set("testD",newValue) } }
var testE:String? { get { return get("testE", nil) } set { set("testE",newValue) } }
var testF:Date? { get { return get("testF", nil) } set { set("testE",newValue) } }
}
要使用此基于字典的"对象,您需要在某个时候创建一个实例并为其提供字典的内容:
To use this "dictionary based" object, you would need to create an instance at some point and give it the dictionary's content:
let simpleHash = SimpleHash(["testA": "A", "testB": "B", "testC": "C"])
simpleHash.testA // "A"
simpleHash.testD // nil
请注意,这不会像使用本机属性并将字典映射到每个物理变量那样高效.另一方面,这样的代码要少得多.如果不经常引用变量,则为简单起见和灵活性,额外的开销可能是可以接受的折衷方案.
Note that, this isn't going to be as efficient as using native properties and mapping the dictionary to each physical variable. On the other hand, it is a lot less code so. If the variables are not referenced often, the extra overhead may be an acceptable trade off for simplicity and flexibility.
这篇关于在Swift 3中,如何将字典转换为Object?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!