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

Optional 类型无默认值问题 #9

Closed
Andy0570 opened this issue Sep 26, 2022 · 2 comments
Closed

Optional 类型无默认值问题 #9

Andy0570 opened this issue Sep 26, 2022 · 2 comments

Comments

@Andy0570
Copy link

有些参数存在服务器字段缺失或没有值的问题,我也不需要设置默认值(或者说,没有办法为某些属性设置一个合理的默认值)。
声明 Optional 类型的属性:

@Codec("pic") var imageURL: URL?

会报编译器红色错误:❌ type of expression is ambiguous without more context

要将属性设置成有默认值的非可选类型,或者:

@Codec("pic") var imageURL: URL? = nil

才可以编译通过。

【问题】Optional 类型的语义就是:要么它存在值,不存在值默认就是 nil,在声明时还需要主动赋值 nil 有点多余。

@winddpan
Copy link
Owner

是有这个问题且我们已经注意到了,但是不太好修复跟另外一个feature冲突。所以目前Optional省略= nil只支持没有key参数的Codec。

// 这么写是可以的
@Codec var imageURL: URL?

// 这么写目前不行
@Codec("pic") var imageURL: URL?

如果未来找到办法的话我们会解决这个用户体验不好的问题,毕竟做这个库的目的是为了提升用户体验。

@winddpan
Copy link
Owner

resolved in 1.0

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