You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1,能否支持默认值的宏扩展。
2,支持Empty 和 快速扩展的Copy 方法。 @codable
@DefaultCodable
public struct Model: Codable, Sendable {
public let name: String
public let title: String
public let createdAt: Int
}
extension Model {
enum CodingKeys: String, CodingKey {
case name
case title
case createdAt
}
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decodeIfPresent(String.self, forKey: .name) ?? ""
self.title = try container.decodeIfPresent(String.self, forKey: .title) ?? ""
self.createdAt = try container.decodeIfPresent(Int.self, forKey: .createdAt) ?? 0
}
public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
try container.encode(title, forKey: .title)
try container.encode(createdAt, forKey: .createdAt)
}
public static let empty = Model(
name: "",
title: "",
createdAt: 0
)
public func copy(
name: String? = nil,
title: String? = nil,
createdAt: Int? = nil
) -> Model {
Model(
name: name ?? self.name,
title: title ?? self.title,
createdAt: createdAt ?? self.createdAt
)
}
}
The text was updated successfully, but these errors were encountered:
1,能否支持默认值的宏扩展。
2,支持Empty 和 快速扩展的Copy 方法。
@codable
@DefaultCodable
public struct Model: Codable, Sendable {
public let name: String
public let title: String
public let createdAt: Int
}
extension Model {
enum CodingKeys: String, CodingKey {
case name
case title
case createdAt
}
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decodeIfPresent(String.self, forKey: .name) ?? ""
self.title = try container.decodeIfPresent(String.self, forKey: .title) ?? ""
self.createdAt = try container.decodeIfPresent(Int.self, forKey: .createdAt) ?? 0
}
public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
try container.encode(title, forKey: .title)
try container.encode(createdAt, forKey: .createdAt)
}
public static let empty = Model(
name: "",
title: "",
createdAt: 0
)
public func copy(
name: String? = nil,
title: String? = nil,
createdAt: Int? = nil
) -> Model {
Model(
name: name ?? self.name,
title: title ?? self.title,
createdAt: createdAt ?? self.createdAt
)
}
}
The text was updated successfully, but these errors were encountered: