update array decoding method

This commit is contained in:
panxi 2019-11-24 23:14:25 -05:00
parent 9ec661ae3b
commit ea69ba67ea
2 changed files with 24 additions and 13 deletions

View File

@ -14,14 +14,20 @@ import UIKit
}
var style: String?
var name: String?
var size: CGFloat?
private enum CodingKeys: String, CodingKey {
case style
case name
case size
}
required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
self.style = try typeContainer.decodeIfPresent(String.self, forKey: .style)
self.name = try typeContainer.decodeIfPresent(String.self, forKey: .name)
self.size = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .size)
try super.init(from: decoder)
}
@ -29,5 +35,7 @@ import UIKit
try super.encode(to: encoder)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(style, forKey: .style)
try container.encodeIfPresent(name, forKey: .name)
try container.encodeIfPresent(size, forKey: .size)
}
}

View File

@ -56,22 +56,25 @@ import Foundation
self.fontName = try typeContainer.decodeIfPresent(String.self, forKey: .fontName)
self.fontSize = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .fontSize)
self.textAlignment = try typeContainer.decodeIfPresent(String.self, forKey: .textAlignment)
do {
var attributesContainer = try typeContainer.nestedUnkeyedContainer(forKey: .attributes)
var attributes = [LabelAttributeModel]()
while !attributesContainer.isAtEnd {
guard let attmodel = try attributesContainer.decodeUnKeyedIfPresent(LabelAttributeModel.self, typeCodingKey: AttributeTypeKey.type) else {
continue
}
attributes.append(attmodel)
}
self.attributes = attributes
} catch {
print("no attributes")
}
self.attributes = try typeContainer.decodeArrayIfPresent(codingKey: .attributes, typeCodingKey: AttributeTypeKey.type) as? [LabelAttributeModel]
self.html = try typeContainer.decodeIfPresent(String.self, forKey: .html)
self.hero = try typeContainer.decodeIfPresent(Int.self, forKey: .hero)
self.makeWholeViewClickable = try typeContainer.decodeIfPresent(Bool.self, forKey: .makeWholeViewClickable)
/*if let atts = self.attributes {
for attribute in atts {
switch attribute.type {
case "font":
let afont = attribute as? LabelAttributeFontModel
print("font length\(afont!.length), \(String(describing: afont?.name))")
case "color":
let afont = attribute as? LabelAttributeColorModel
print("font length\(afont!.length), \(String(describing: afont?.textColor))")
default:
print("attribute")
}
}
} */
}
public func encode(to encoder: Encoder) throws {