winddpan / codablewrapper Goto Github PK
View Code? Open in Web Editor NEW@CodingKey("encoder", "decoder") var cool: Bool = true
License: MIT License
@CodingKey("encoder", "decoder") var cool: Bool = true
License: MIT License
@Codable
struct ImproveHightligh {
var name: String?
}
@Codable
struct ImproveSuggestion: Equatable {
var name = ""
let code: String?
var highlightList: [ImproveHightligh]?
static func == (lhs: ImproveSuggestion, rhs: ImproveSuggestion) -> Bool {
return lhs.name == rhs.name && lhs.code == rhs.code
}
}
这种写法会报错:Type 'ImproveSuggestion' does not conform to protocol 'Equatable'
有两种方式可以解决,我目前使用了extension的方式处理
/// 方式1,为模型ImproveHightligh添加Equtable
@Codable
struct ImproveHightligh: Equatable {
var name: String?
}
/// 方式2,使用扩展
extension ImproveSuggestion: Equatable {
static func == (lhs: ImproveSuggestion, rhs: ImproveSuggestion) -> Bool {
return lhs.name == rhs.name && lhs.code == rhs.code
}
}
推测可能是Equtable本身会对代码进行解析并自动实现func ==,Equtable解析的过程与与宏定义有冲突,可能是SwiftMacro的bug
因为用到了mirror反查系统内部属性,thread注入参数这些不稳定的实现。和swift追求静态语言的原则不太符,活活把swift写错了oc
遇到几个问题:
1, 如果一个model, 在不同的场合下要转换成不同的JSON, 这个怎么处理?
2,property wrapper无法嵌套, 如果用了这个库, 那么model里边就没办法再使用其它库的property wrapper了。
Reproduce sample code:
@codable
class Foo: Codable {
var bar: String? = ""
var type: String = ""
}
when build xcode error message:
Use of 'type' refers to instance method rather than global function 'type(of:)' in module 'Swift'
Xcode版本 15.0.1,playground和project文件都无法打开
请问model里面是枚举如何处理呢?
有些参数存在服务器字段缺失或没有值的问题,我也不需要设置默认值(或者说,没有办法为某些属性设置一个合理的默认值)。
声明 Optional 类型的属性:
@Codec("pic") var imageURL: URL?
会报编译器红色错误:❌ type of expression is ambiguous without more context
。
要将属性设置成有默认值的非可选类型,或者:
@Codec("pic") var imageURL: URL? = nil
才可以编译通过。
【问题】Optional 类型的语义就是:要么它存在值,不存在值默认就是 nil
,在声明时还需要主动赋值 nil
有点多余。
这。。还能继续在正式环境里用吗?
新版本改动这么大..一更新都报错..
@CodableWrapper 命名改了么?
(defaultValue: 也改了么?替代的用法是啥?
-> CodableWrapper (0.3.2)
Codable + PropertyWrapper
pod 'CodableWrapper', '~> 0.3.2'
@CodingKey("shelteredplace") let bottomApp: String
@CodingKey("undid") let bottomNet: String
var enable: Bool {
if optional == 1 {
return true
}
if !bottomNet.isEmpty {
return true
}
return false
}
lazy var pubEnable: CurrentValueSubject<Bool, Never> = {
let subject = CurrentValueSubject<Bool, Never>(enable)
return subject
}()
如上面的模型中pubEnable
,如是一个懒加载的属性,我怎么忽略这个属性
相比 BetterCodable 等其他库的使用上来说, 简化了很多, 作者好棒!!!
如题
现在的项目,都是从历史来的,要完全脱离cocoapods还是很难的,但是我们的版本是符合的,最低安装版本为iOS13,我们希望能在新需求中,使用swift5.9的新特性来解析codable,以提高解析安全性。如果cocoapods和spm混用的话,那我的cocoapods子库无法引用到CodableWrapper,所以只能使用cocoapods来接入。
如果CodableWrapper的cocoapods的版本不能更新,那我们不会考虑接入CodableWrapper
希望作者考虑一下这个问题,感谢!
`struct ExaminationItem: Codable {
static let tableName = "ExamTable"
var id: String
var type: String
var typename: String
var fathertype: String
var fathertypename: String
var searchwd: String?
var biaozhu: String?
var timu: [ExamOptionItem] = []
var xuanxiang: [ExamOptionItem] = []
var jiexi: [ExamOptionItem] = []
var daan: [String] = []
@Codec var showAnalyze: Bool = false
@Codec var isshoucang: Bool = false
@Codec var timuleibie: Bool = false
var noXunxiang: Bool {
get {
return daan.count <= 0
}
}
var isRadio: Bool {
get {
return daan.count <= 1
}
}
}
extension ExaminationItem: TableCodable {
enum CodingKeys: String, CodingTableKey {
typealias Root = ExaminationItem
static let objectRelationalMapping = TableBinding(CodingKeys.self)
case id
case type
case typename
case fathertype
case fathertypename
case searchwd
case biaozhu
case timu
case xuanxiang
case jiexi
case daan
case isshoucang
case timuleibie
}
}`
请教下 需要怎么解决
var a = b
a.p = 1
此时b.p也变成了1
pod 'CodableWrapper', :git => 'https://github.com/winddpan/CodableWrapper.git', :branch => '1.1.0'
Version 15.0.1 (15A507)
耗时,cpu拉满,升温,编译失败
比如Swift原生的Codable与其他第三方的比较
CodableWrapper的0.3.3版本与CleanJSON的1.0.9版本一起用,因为大家都重写了decode过程,导致Crash
use "@codec var stringVal: String = "OK""
error: "Unknown attribute 'Codec'"
这。。还能继续在正式环境里用吗?
新版本改动这么大..一更新都报错..
@CodableWrapper 命名改了么?
(defaultValue: 也改了么?替代的用法是啥?
如下列表,我想要排除createAt
属性的序列化以及反序列化
struct Article {
@Codec var title: String?
@Codec var info: String?
@Codec var desc: String?
@Codec var createAt: String?
}
Thank you for designing such a beautiful and easy to use library.
The inability to parse enumerations limits their use.
@codable
class BaseModel{
var errorno: Int = -1
var error: String = ""
// var state: String?//有state参数就运行不起来
}
在一些简单的model添加了 @codable 宏后,xcode run的时候一直过不去,很奇怪为什么 ,也不报错
import CodableWrapper
struct LocationModel: Codable {
@Codec("Id")
var id: String? = nil
@Codec("Value")
var value: String? = nil
@Codec("Children")
var child: [LocationModel]? = nil
}
Test:
let jsonString2 =
"[{\"Value\":null,\"Label\":\"State/Province\",\"Id\":\"a0Mp0000007ANEoEAO\",\"Children\":[{\"Value\":\"Bangkok\",\"Label\":\"City\",\"Id\":\"a0Mp0000007ANe7EAG\",\"Children\":[{\"Value\":\"Bang Kapi\",\"Label\":\"District\",\"Id\":\"a0Mp0000007ANeCEAW\",\"Children\":null}]}]}]".data(using: .utf8)!
let decoder = JSONDecoder()
let model = try! decoder.decode([LocationModel].self, from: jsonString2)
logInfo("json = \(model.toJSONString())")
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.