Swift - Encode and Decode a dictionary [String:Any] into plist

前端 未结 2 1444
天涯浪人
天涯浪人 2021-01-17 05:00

I am trying to store the dictionary in my class Marker but it is throwing an error saying it is not encodable or decodable. I can see the error is caused by the [String: Any

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-17 05:15

    So finally worked it out with the help of Andrada.

    I added a second struct which held the action and by passed having to use [string:any]

    class Marker : Encodable, Decodable {
    var UUIDpic: UUID = UUID()
    var alpha: Int = 1
    var buttonType: Int = 0
    var buttonAction : [String: [ButtonAction]] = [:] //Dictionary I edited using the new struct
    var buttonNameColor: String = ""
    var buttonNameFontSize: Int = 10
    var buttonShape: String = ""
    var loggerRect: String = ""
    var maskColor: String = ""
    var name: String = ""
    }
    

    Below is the struct I added

    struct ButtonAction: Codable {
    var action: String
    var array_linked_of_buttons: [[String:String]]
    
    init(action: String, array_linked_of_buttons: [[String:String]]) {
     self.action = action
     self.array_linked_of_buttons = array_linked_of_buttons
        }
    }
    

    Make sure to init your struct or it won't work.

提交回复
热议问题