Xcode 11, Build failure, getting the error message: "Undefined symbol: propwrapper2.counter.getter : Swift.Int"

Dear All, I started a small project to play with the new propertyWrapper feature.

I used a simple sample found to start and got the build error : "Undefined symbol: propwrapper2.counter.getter : Swift.Int" (from the linker)

I cannot figure out how to solve this.

Thank you.



I started a new command line tool project, and please find here is the simple code which is failing at build:


import Foundation

@propertyWrapper
struct Constrained {
    private let defaultValue : Value

    init ( defaultValue: Value ) {
        self.defaultValue = defaultValue
        self.wrappedValue = defaultValue
    }

    var wrappedValue: Value {
        didSet {
            if self.wrappedValue == 0 {
                self.wrappedValue = self.defaultValue
            }
        }
    }
}

@Constrained ( defaultValue: 1)
var counter: Int

counter = 14
print (counter)

counter = 0
print (counter)

counter = 4
print (counter)

Replies

AFAIK, you should add a getter


Such as in this tutorial:

h ttps://nshipster.com/propertywrapper/

    var wrappedValue: Value {
        get { value }
        set { value = min(max(range.lowerBound, newValue), range.upperBound) }
    }

Found my mistake, thanks to new XCode 11b5.


- The new compiler complains because I cannot use the property wrapper with a global scope

- Then, I tried to embed the last 8 lines of code into a function, the compiler this time complains that the current implementation of property wrappers cannot be used as local variables.


Ok, lesson learned, property wrapper is to wrap a property (within a struct,...) and NOT for wrapping a variable such as a global or local variable.

Modifying the code by using a property wrapper inside a struct made the code working.

property wrapper is to wrap a property (within a struct,...) and NOT for wrapping a variable such as a global or local variable.

Right now, yes, but that should change. To quote SE-0258 Property Wrappers: Property wrappers can be applied to properties at global, local, or type scope.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"