Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Compilation Error for Property Wrappers with Codable in multiple files

Compilation failed for property wrappers with codable in multiple files.

I found test codes in Swift source below:

  • property_wrappers_codable_multifile_other.swift
@propertyWrapper
struct Printed<Value: Codable>: Codable {
    var wrappedValue: Value {
        didSet { print(wrappedValue) }
    }
}

struct Foo: Codable {
    @Printed var bar: Bool = false
}
  • property_wrappers_codable_multifile.swift
func test(_ value: Foo = Foo()) {
  let _: Codable = value
}

and use them in my test project:

TestProject

But compilation failed with error:

Type 'Foo' does not conform to protocol 'Encodable'

How to fix it?

like image 328
lucky.li Avatar asked Nov 20 '25 11:11

lucky.li


1 Answers

It is a question of visibility... the simplest fix is just to move those helper structs into ViewController module as below... run, and all works (tested with Xcode 11.2)

enter image description here

like image 51
Asperi Avatar answered Nov 22 '25 04:11

Asperi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!