使 CAShapeLayer 和 CGPath 符合 Codable 或 NSCoding
make CAShapeLayer and CGPath conform to Codable or to NSCoding
我正在尝试使用 Codable 协议在 .json 个文件中保存和获取有关 CAShapeLayers 的信息,但出现错误:
public class Node: Codable {
public var name: String = ""
public var path: CGPath?
public var baseLayer: CAShapeLayer?
public required init(from decoder: Decoder) throws {
let container = try? decoder.container(keyedBy: NodeCodingKeys.self)
let _name = try container?.decodeIfPresent(String.self, forKey: NodeCodingKeys.name)
let _baseLayer = try container?.decodeIfPresent(CAShapeLayer.self, forKey: NodeCodingKeys.baseLayer)
name = _name ?? ""
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: NodeCodingKeys.self)
try container.encode(name, forKey: NodeCodingKeys.name)
}
}
public enum NodeCodingKeys: CodingKey {
case path
case name
case baseLayer
}
CGPath 也一样。有没有办法save/receive他们to/from设备?
我找不到使 CAShapeLayer
符合 Codable
的方法,所以我为我的数据对象创建了 class:
public class ImageNode: NSObject, NSCoding, NSSecureCoding {
public static var supportsSecureCoding = true
var name: NSString
var baseLayer: CAShapeLayer
init(name: NSString,
baseLayer: CAShapeLayer
) {
self.name = name
self.baseLayer = baseLayer
}
public func encode(with coder: NSCoder) {
coder.encode(name, forKey: "name")
coder.encode(baseLayer, forKey: "baseLayer")
}
public required convenience init?(coder: NSCoder) {
guard let name = coder.decodeObject(forKey: "name") as? NSString
else { return nil }
guard let baseLayer = coder.decodeObject(forKey: "baseLayer") as? CAShapeLayer
else { return nil }
print("decode node")
self.init(
name: name,
baseLayer: baseLayer
)
} }
与 NSKeyedArchiver
和 NSKeyedUnarchiver
配合使用可以将数据保存到文件并进行检索。也许这个解决方案会对某人有所帮助。
我正在尝试使用 Codable 协议在 .json 个文件中保存和获取有关 CAShapeLayers 的信息,但出现错误:
public class Node: Codable {
public var name: String = ""
public var path: CGPath?
public var baseLayer: CAShapeLayer?
public required init(from decoder: Decoder) throws {
let container = try? decoder.container(keyedBy: NodeCodingKeys.self)
let _name = try container?.decodeIfPresent(String.self, forKey: NodeCodingKeys.name)
let _baseLayer = try container?.decodeIfPresent(CAShapeLayer.self, forKey: NodeCodingKeys.baseLayer)
name = _name ?? ""
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: NodeCodingKeys.self)
try container.encode(name, forKey: NodeCodingKeys.name)
}
}
public enum NodeCodingKeys: CodingKey {
case path
case name
case baseLayer
}
CGPath 也一样。有没有办法save/receive他们to/from设备?
我找不到使 CAShapeLayer
符合 Codable
的方法,所以我为我的数据对象创建了 class:
public class ImageNode: NSObject, NSCoding, NSSecureCoding {
public static var supportsSecureCoding = true
var name: NSString
var baseLayer: CAShapeLayer
init(name: NSString,
baseLayer: CAShapeLayer
) {
self.name = name
self.baseLayer = baseLayer
}
public func encode(with coder: NSCoder) {
coder.encode(name, forKey: "name")
coder.encode(baseLayer, forKey: "baseLayer")
}
public required convenience init?(coder: NSCoder) {
guard let name = coder.decodeObject(forKey: "name") as? NSString
else { return nil }
guard let baseLayer = coder.decodeObject(forKey: "baseLayer") as? CAShapeLayer
else { return nil }
print("decode node")
self.init(
name: name,
baseLayer: baseLayer
)
} }
与 NSKeyedArchiver
和 NSKeyedUnarchiver
配合使用可以将数据保存到文件并进行检索。也许这个解决方案会对某人有所帮助。