TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

NSDictionary with some values as functions and UIImages to swift object. Help needed!

Forums > Swift

Hello! Would appreciate any advices!

I have an issue to solve and it seems I can't find the answer anywhere. Some context: we are updating our ios app and the production version is written in obj-c, but new UI features we develop in Swift. Therefore we have a dictionary with some values for our Swift side of development which contains a lot of important data for the UI. As for now we are just looping the dict to fetch the values. I want to convert it to a swift object, so it would be easier to access the needed values. The problem is that the dictionary contains values that are function calls and UIImage type. What would be the smartest way to parse this dictionary to a swift object? The dictionary:

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":"Rusys",
         //   "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")
    //callQRScanner()
    //GlobalStruct.editIsPressed = true
}

func menuButtonTapCallback() {
    print("menu button tapped ")

}

func buttonTapCallback(){
    print("button tapped")
    //mainVC?.view.showMessageView(title: "sss",text: "kazkas")
}

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
}

2      

my question was answered in stackoverflow, if anyone is looking for a solution: https://stackoverflow.com/questions/63225669/decode-nsdictionary-with-functions-inside-as-values-to-swift-object

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.