使用内部函数解码 NSDictionary 作为 swift 对象的值
Decode NSDictionary with functions inside as values to swift object
如有任何意见或建议,我们将不胜感激!谢谢!
我有一个问题要解决,但似乎到处都找不到答案。一些上下文:我们正在更新我们的 ios 应用程序,生产版本是用 obj-c 编写的,但是我们在 Swift 中开发的新 UI 功能。因此,我们有一个字典,其中包含 Swift 开发方面的一些值,其中包含 UI 的许多重要数据。至于现在我们只是循环字典来获取值。我想将它转换为 swift 对象,这样访问所需的值会更容易。问题是字典包含函数调用和 UIImage 类型的值。将这个字典解析为 swift 对象的最聪明的方法是什么?词典:
let dict : [String: Any] = [
"keySelectedMenuButton" : 0,
"keySettButtCallback" : settTapCallback,
"keyInfoButtCallback" : logoTapCallback,
"keyObjectMenuCallback": menuButtonTapCallback,
"buttonListChanged" : doneEditTapCallback,
"keyObjectArray" : [
["keyObjectTitle" : "Home",
// "keyObjectLineImage": UIImage(named: "objektu atskyrimas_4x")!,
"keyObjCtrlButtCollectionArray" : [
["keyCtrlItemTitle" : "Smart Menu",
"keyCtrlItemSubtitle" : "",
//"keyCtrlItemImage" : UIImage(named: "ico_rocket")!,
"keyCtrlObjectAfterTap" : buttonTap1Callback
],
["keyCtrlItemTitle" : "Lock",
"keyCtrlItemSubtitle" : "doors",
//"keyCtrlItemImage" : UIImage(named: "ico_lock_1@x3")!,
"keyCtrlObjectAfterTap" : buttonTapCallback
],
["keyCtrlItemTitle" : "Unlock",
"keyCtrlItemSubtitle" : "Gates",
// "keyCtrlItemImage" : UIImage(named: "ico_lock_0@x3")!,
"keyCtrlObjectAfterTap" : buttonTapCallback
],
["keyCtrlItemTitle" : "Ignore",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_shield@x3")!,
"keyCtrlObjectAfterTap" : buttonTap1Callback
],
["keyCtrlItemTitle" : "Stop",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_stop")!
"keyCtrlObjectAfterTap" : buttonTap2Callback
],
["keyCtrlItemTitle" : "State",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_query@x3")!,
"keyCtrlObjectAfterTap" : buttonTap3Callback
],
["keyCtrlItemTitle" : "Controller",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_settings_lines")!,
"keyCtrlObjectAfterTap" : buttonTap1Callback
],
["keyCtrlItemTitle" : "Logs",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_log@x3")!,
"keyCtrlObjectAfterTap" : buttonTap2Callback
],
["keyCtrlItemTitle" : "Notifications",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_notifications")!,
"keyCtrlObjectAfterTap" : buttonTap3Callback
]]
],
["keyObjectTitle":"Auto",
//"keyObjectLineImage": UIImage(named: "objektu atskyrimas_4x")!,
"keyObjCtrlButtCollectionArray" : [
["keyCtrlItemTitle" : "Smart Menu",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_rocket")!,
"keyCtrlObjectAfterTap" : buttonTap2Callback
],
["keyCtrlItemTitle" : "Lock",
"keyCtrlItemSubtitle" : "Backdoor",
// "keyCtrlItemImage" : UIImage(named: "ico_lock_1@x3")!,
"keyCtrlObjectAfterTap" : buttonTapCallback
],
["keyCtrlItemTitle" : "Unlock",
"keyCtrlItemSubtitle" : "Fence",
// "keyCtrlItemImage" : UIImage(named: "ico_lock_0@x3")!,
"keyCtrlObjectAfterTap" : buttonTapCallback
],
["keyCtrlItemTitle" : "Ignore",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_shield@x3")!,
"keyCtrlObjectAfterTap" : buttonTap1Callback
],
["keyCtrlItemTitle" : "Stop",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_stop")!,
"keyCtrlObjectAfterTap" : buttonTap3Callback
]]
],
["keyObjectTitle":"Basement",
// "keyObjectLineImage": UIImage(),
"keyObjCtrlButtCollectionArray" : [
["keyCtrlItemTitle" : "Smart Menu",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_rocket")!,
"keyCtrlObjectAfterTap" : buttonTap1Callback
],
["keyCtrlItemTitle" : "Lock",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_lock_1@x3")!,
"keyCtrlObjectAfterTap" : buttonTapCallback
],
["keyCtrlItemTitle" : "Unlock",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_lock_0@x3")!,
"keyCtrlObjectAfterTap" : buttonTapCallback
]]
]
]
]
func logoTapCallback(){
print("logo tapped")
}
func menuButtonTapCallback() {
print("menu button tapped ")
}
func buttonTapCallback(){
print("button tapped")
}
func buttonTap1Callback(){
print("button tapped 1")
}
func buttonTap2Callback(){
print("button tapped 2")
}
func buttonTap3Callback(){
print("button tapped 3")
}
func settTapCallback(){
print("settTap")
}
func doneEditTapCallback(){
print("doneEditTap")
}
// Dictionary structure I would like to use:
struct InitialDictionaryStructure {
var keySelectedMenuButton : Int
var keySettButtCallback : ()->Void
var keyInfoButtCallback : ()->Void
var keyObjectMenuCallback : ()->Void
var buttonListChanged : ()->Void
var keyObjectArray : [[Object]]
}
struct Object {
var keyObjectTitle : String
//var keyObjectLineImage : UIImage
var keyObjCtrlButtCollectionArray : [Button]
}
struct Button {
var keyCtrlItemTitle : String
var keyCtrlItemSubtitle : String
//var keyCtrlItemImage : UIImage
var keyCtrlObjectAfterTap : ()-> Void
}
没有智能方式,你必须添加初始化器。
如果不修改值,则将结构成员声明为常量,我略微缩短了成员名称,省略了冗余信息。
struct InitialDictionaryStructure {
let menuButton : Int
let settButtCallback : ()->Void
let infoButtCallback : ()->Void
let objectMenuCallback : ()->Void
let buttonListChanged : ()->Void
let objects : [Object]
init(dictionary : [String:Any]) {
menuButton = dictionary["keySelectedMenuButton"] as! Int
settButtCallback = dictionary["keySettButtCallback"] as! ()-> Void
infoButtCallback = dictionary["keyInfoButtCallback"] as! ()-> Void
objectMenuCallback = dictionary["keyObjectMenuCallback"] as! ()-> Void
buttonListChanged = dictionary["buttonListChanged"] as! ()-> Void
let objectData = dictionary["keyObjectArray"] as! [[String:Any]]
objects = objectData.map(Object.init)
}
}
struct Object {
let title : String
// let lineImage : UIImage
let buttons : [Button]
init(dictionary : [String:Any]) {
title = dictionary["keyObjectTitle"] as! String
// lineImage = dictionary["keyObjectLineImage"] as! UIImage
let buttonData = dictionary["keyObjCtrlButtCollectionArray"] as! [[String:Any]]
buttons = buttonData.map(Button.init)
}
}
struct Button {
let title : String
let subtitle : String
// let image : UIImage
let objectAfterTap : ()-> Void
init(dictionary : [String:Any]) {
title = dictionary["keyCtrlItemTitle"] as! String
subtitle = dictionary["keyCtrlItemSubtitle"] as! String
// image = dictionary["keyCtrlItemImage"] as! UIImage
objectAfterTap = dictionary["keyCtrlObjectAfterTap"] as! ()-> Void
}
}
请考虑这是一个没有任何类型和 nil
检查的简单示例。如果字典不包含具有正确类型的所有键,代码将崩溃。
要创建项目写入
let initialDictionaryStructure = InitialDictionaryStructure(dictionary: dict)
print(initialDictionaryStructure)
如有任何意见或建议,我们将不胜感激!谢谢!
我有一个问题要解决,但似乎到处都找不到答案。一些上下文:我们正在更新我们的 ios 应用程序,生产版本是用 obj-c 编写的,但是我们在 Swift 中开发的新 UI 功能。因此,我们有一个字典,其中包含 Swift 开发方面的一些值,其中包含 UI 的许多重要数据。至于现在我们只是循环字典来获取值。我想将它转换为 swift 对象,这样访问所需的值会更容易。问题是字典包含函数调用和 UIImage 类型的值。将这个字典解析为 swift 对象的最聪明的方法是什么?词典:
let dict : [String: Any] = [
"keySelectedMenuButton" : 0,
"keySettButtCallback" : settTapCallback,
"keyInfoButtCallback" : logoTapCallback,
"keyObjectMenuCallback": menuButtonTapCallback,
"buttonListChanged" : doneEditTapCallback,
"keyObjectArray" : [
["keyObjectTitle" : "Home",
// "keyObjectLineImage": UIImage(named: "objektu atskyrimas_4x")!,
"keyObjCtrlButtCollectionArray" : [
["keyCtrlItemTitle" : "Smart Menu",
"keyCtrlItemSubtitle" : "",
//"keyCtrlItemImage" : UIImage(named: "ico_rocket")!,
"keyCtrlObjectAfterTap" : buttonTap1Callback
],
["keyCtrlItemTitle" : "Lock",
"keyCtrlItemSubtitle" : "doors",
//"keyCtrlItemImage" : UIImage(named: "ico_lock_1@x3")!,
"keyCtrlObjectAfterTap" : buttonTapCallback
],
["keyCtrlItemTitle" : "Unlock",
"keyCtrlItemSubtitle" : "Gates",
// "keyCtrlItemImage" : UIImage(named: "ico_lock_0@x3")!,
"keyCtrlObjectAfterTap" : buttonTapCallback
],
["keyCtrlItemTitle" : "Ignore",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_shield@x3")!,
"keyCtrlObjectAfterTap" : buttonTap1Callback
],
["keyCtrlItemTitle" : "Stop",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_stop")!
"keyCtrlObjectAfterTap" : buttonTap2Callback
],
["keyCtrlItemTitle" : "State",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_query@x3")!,
"keyCtrlObjectAfterTap" : buttonTap3Callback
],
["keyCtrlItemTitle" : "Controller",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_settings_lines")!,
"keyCtrlObjectAfterTap" : buttonTap1Callback
],
["keyCtrlItemTitle" : "Logs",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_log@x3")!,
"keyCtrlObjectAfterTap" : buttonTap2Callback
],
["keyCtrlItemTitle" : "Notifications",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_notifications")!,
"keyCtrlObjectAfterTap" : buttonTap3Callback
]]
],
["keyObjectTitle":"Auto",
//"keyObjectLineImage": UIImage(named: "objektu atskyrimas_4x")!,
"keyObjCtrlButtCollectionArray" : [
["keyCtrlItemTitle" : "Smart Menu",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_rocket")!,
"keyCtrlObjectAfterTap" : buttonTap2Callback
],
["keyCtrlItemTitle" : "Lock",
"keyCtrlItemSubtitle" : "Backdoor",
// "keyCtrlItemImage" : UIImage(named: "ico_lock_1@x3")!,
"keyCtrlObjectAfterTap" : buttonTapCallback
],
["keyCtrlItemTitle" : "Unlock",
"keyCtrlItemSubtitle" : "Fence",
// "keyCtrlItemImage" : UIImage(named: "ico_lock_0@x3")!,
"keyCtrlObjectAfterTap" : buttonTapCallback
],
["keyCtrlItemTitle" : "Ignore",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_shield@x3")!,
"keyCtrlObjectAfterTap" : buttonTap1Callback
],
["keyCtrlItemTitle" : "Stop",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_stop")!,
"keyCtrlObjectAfterTap" : buttonTap3Callback
]]
],
["keyObjectTitle":"Basement",
// "keyObjectLineImage": UIImage(),
"keyObjCtrlButtCollectionArray" : [
["keyCtrlItemTitle" : "Smart Menu",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_rocket")!,
"keyCtrlObjectAfterTap" : buttonTap1Callback
],
["keyCtrlItemTitle" : "Lock",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_lock_1@x3")!,
"keyCtrlObjectAfterTap" : buttonTapCallback
],
["keyCtrlItemTitle" : "Unlock",
"keyCtrlItemSubtitle" : "",
// "keyCtrlItemImage" : UIImage(named: "ico_lock_0@x3")!,
"keyCtrlObjectAfterTap" : buttonTapCallback
]]
]
]
]
func logoTapCallback(){
print("logo tapped")
}
func menuButtonTapCallback() {
print("menu button tapped ")
}
func buttonTapCallback(){
print("button tapped")
}
func buttonTap1Callback(){
print("button tapped 1")
}
func buttonTap2Callback(){
print("button tapped 2")
}
func buttonTap3Callback(){
print("button tapped 3")
}
func settTapCallback(){
print("settTap")
}
func doneEditTapCallback(){
print("doneEditTap")
}
// Dictionary structure I would like to use:
struct InitialDictionaryStructure {
var keySelectedMenuButton : Int
var keySettButtCallback : ()->Void
var keyInfoButtCallback : ()->Void
var keyObjectMenuCallback : ()->Void
var buttonListChanged : ()->Void
var keyObjectArray : [[Object]]
}
struct Object {
var keyObjectTitle : String
//var keyObjectLineImage : UIImage
var keyObjCtrlButtCollectionArray : [Button]
}
struct Button {
var keyCtrlItemTitle : String
var keyCtrlItemSubtitle : String
//var keyCtrlItemImage : UIImage
var keyCtrlObjectAfterTap : ()-> Void
}
没有智能方式,你必须添加初始化器。
如果不修改值,则将结构成员声明为常量,我略微缩短了成员名称,省略了冗余信息。
struct InitialDictionaryStructure {
let menuButton : Int
let settButtCallback : ()->Void
let infoButtCallback : ()->Void
let objectMenuCallback : ()->Void
let buttonListChanged : ()->Void
let objects : [Object]
init(dictionary : [String:Any]) {
menuButton = dictionary["keySelectedMenuButton"] as! Int
settButtCallback = dictionary["keySettButtCallback"] as! ()-> Void
infoButtCallback = dictionary["keyInfoButtCallback"] as! ()-> Void
objectMenuCallback = dictionary["keyObjectMenuCallback"] as! ()-> Void
buttonListChanged = dictionary["buttonListChanged"] as! ()-> Void
let objectData = dictionary["keyObjectArray"] as! [[String:Any]]
objects = objectData.map(Object.init)
}
}
struct Object {
let title : String
// let lineImage : UIImage
let buttons : [Button]
init(dictionary : [String:Any]) {
title = dictionary["keyObjectTitle"] as! String
// lineImage = dictionary["keyObjectLineImage"] as! UIImage
let buttonData = dictionary["keyObjCtrlButtCollectionArray"] as! [[String:Any]]
buttons = buttonData.map(Button.init)
}
}
struct Button {
let title : String
let subtitle : String
// let image : UIImage
let objectAfterTap : ()-> Void
init(dictionary : [String:Any]) {
title = dictionary["keyCtrlItemTitle"] as! String
subtitle = dictionary["keyCtrlItemSubtitle"] as! String
// image = dictionary["keyCtrlItemImage"] as! UIImage
objectAfterTap = dictionary["keyCtrlObjectAfterTap"] as! ()-> Void
}
}
请考虑这是一个没有任何类型和 nil
检查的简单示例。如果字典不包含具有正确类型的所有键,代码将崩溃。
要创建项目写入
let initialDictionaryStructure = InitialDictionaryStructure(dictionary: dict)
print(initialDictionaryStructure)