Skip to content

Instantly share code, notes, and snippets.

@sgr-ksmt
Last active November 2, 2019 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgr-ksmt/96229dd8ebd5fc17755fdbd0853e805d to your computer and use it in GitHub Desktop.
Save sgr-ksmt/96229dd8ebd5fc17755fdbd0853e805d to your computer and use it in GitHub Desktop.
PropertyWrapper+Codable crash.
struct ModelA: Codable {
@Wrap<ModelA> var a: String? = "foobar"
}
struct ModelB: Codable {
@Wrap<ModelB> var b: String? // doesn't work if inital value given (e.g. `@Wrap<ModelB> var b: String? = "foobar"`)
}
struct Test {
func test() {
_ = try? JSONEncoder().encode(ModelA()) // doesn't work.
_ = try? JSONEncoder().encode(ModelB()) // works.
}
}
@propertyWrapper
struct Wrap<T>: Codable where T: Codable {
private var path: String?
init(wrappedValue path: String?) {
self.path = path
}
var wrappedValue: String? {
get { path }
set { path = newValue}
}
var projectedValue: Self {
self
}
}
@sgr-ksmt
Copy link
Author

sgr-ksmt commented Nov 2, 2019

  • Compile Error will cause if define each class to different files.
  • Compile Error will not cause if define each class to same file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment