0

I have created a model to map my Api response to the swift struct with Codable protocol. but it always return nil. Note: - if i remove the isSelected from the model it works perfect. But I need this key to manage the selection from user. Also if i try this approach with class it always return self is immutable error. did any one face this issue also please help me to convert this code to swift class

struct CustomCodingKey:CodingKey {
        var stringValue: String
        var intValue: Int?

        init?(stringValue: String) {
            self.stringValue = stringValue
            self.intValue = nil
        }

        init?(intValue: Int) {
            self.stringValue = String(intValue)
            self.intValue = intValue
        }
    }

    struct Field: Codable {

        var type: String
        var label: String
        var name: String
        var isSelected = false

        init?(json: JSON) {
            do {
                let data = try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
                let decoder = JSONDecoder.init()
                decoder.keyDecodingStrategy = .custom { keys -> CodingKey in
                    let key = keys.last!.stringValue.split(separator: "-").joined()
                    print(key)
                    return CustomCodingKey(stringValue: String(key))!
                }
                self = try decoder.decode(Field.self, from: data)
            } catch {
                return nil
            }
        }

        var json: JSON {
            do {
                let data = try JSONEncoder().encode(self)
                if let tempJson = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? JSON {
                    return tempJson
                }
                return JSON()
            } catch {
                return JSON()
            }

        }
    }
Ajay Singh Mehra
  • 1,313
  • 9
  • 19
  • Why do you ignore the `error` passed in the `catch` blocks? Most likely it gives you the helpful hint. By the way: Why `.prettyPrinted`? The decoder doesn't care about *calligraphy* and why `.mutableContainers`? It's pointless in Swift and you are assigning the result to an immutable **constant** anyway. – vadian Apr 02 '18 at 11:34
  • `.prettyPrinted` is used to covert Dictionary into data. as decoder work on the data. `.mutableContainers` is used for the data to dictionary conversion. The problem is without isSelected variable it works perfect. – Ajay Singh Mehra Apr 02 '18 at 11:46
  • immutable error is coming in case when we move model from struct to class – Ajay Singh Mehra Apr 02 '18 at 11:48
  • Both options are completely useless. In Swift you cannot assign something to `self` in an `init` method. This is not Objective-C. once again: Handle the actual errors in the `catch` blocks. – vadian Apr 02 '18 at 11:54
  • @vadian for struct in swift we could assign the self in init. if swift doesn't allow that, than complier will throw the error. with class we can't assign self to init method. – Ajay Singh Mehra Apr 02 '18 at 12:04
  • my question is that if there is no key in the json for isselected than why the codeable protocol not taking the default value. – Ajay Singh Mehra Apr 02 '18 at 12:05
  • @vadian also we can't convert JSON directly into Data. so we have to convert it through json serialisation or we could use nsarchive for the same. – Ajay Singh Mehra Apr 02 '18 at 12:35
  • Yes, but once again: The option `prettyPrinted` As well as `mutableContainers` is meaningless. In both cases pass *no options* (omit the parameter). – vadian Apr 02 '18 at 12:43

0 Answers0