Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model中的属性为可选类型的时候,JSON数据没有对应的字段,最后从model中取属性没有默认值 #20

Closed
yangguanghei opened this issue Sep 22, 2021 · 5 comments

Comments

@yangguanghei
Copy link

struct CleanJSONModel: Codable {
var name: String?
}

if model.name == nil {
debugPrint("没有name")
}

@Pircate
Copy link
Owner

Pircate commented Sep 22, 2021

可选属性的默认值是nil,想要默认值请使用非可选类型

@yangguanghei
Copy link
Author

可选属性的默认值是nil,想要默认值请使用非可选类型

如果属性使用可选类型,类型不匹配的时候该属性的结果也是nil。感觉如果使用Clean JSON得尽量使用不可选类型。

@Pircate
Copy link
Owner

Pircate commented Sep 22, 2021

嗯嗯,尽量使用不可选类型。除非你真的需要判断是否为nil

@yangguanghei
Copy link
Author

嗯嗯,尽量使用不可选类型。除非你真的需要判断是否为nil

项目中之前用的是Codable Wrapper,现在准备用你的Clean Json,感觉使用更简单,对源码侵入也少,👍

@Pircate
Copy link
Owner

Pircate commented Sep 22, 2021

感谢使用,有问题可以反馈给我

@Pircate Pircate closed this as completed Oct 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants