PropertyWrappers in Xcode 11

Has anyone successfully defined and used a property wrapper in Xcode 11?

I have tried, using the Clamping example in the proposal. If I don't use the property, it builds fine. However, if I try to define a property using the Clamping, then Xcode cannot compile the project, rather it seems to become stuck indexing files.

Yes, it works for me. The only issue I ran into was when I was using a protocol, like @ObjectBinding var foo: FooProtocol instead of @ObjectBinding var foo: ConformsToFooProtocol.

Can you post your code?

Here's the initial prototype:

import Foundation

@propertyWrapper
struct Clamping<V: Comparable> {
    var value: V
    let min: V
    let max: V
    
    init(initialValue: V, min: V, max: V) {
        self.value = initialValue
        self.min = min
        self.max = max
        assert(self.value >= self.min && self.value <= max)
    }
    
    var wrappedValue: V {
        get { return value }
        set {
            if newValue < min {
                value = min
            } else if newValue > max {
                value = max
            } else {
                value = newValue
            }
        }
    }
}

struct Color {
    @Clamping(min: 0, max: 255) var red: Int = 127
    @Clamping(min: 0, max: 255) var green: Int = 127
    @Clamping(min: 0, max: 255) var blue: Int = 127
    @Clamping(min: 0, max: 255) var alpha: Int = 255
}

If I comment out the definition of Color, it builds fine. As soon as I put it back in, I have problems.

Hmm interesting, I can reproduce your issue on Xcode 11 beta 2. Could you file a bug report at bugs.swift.org?

Yes, I will file a bug. However, I'm curious what code propertyWrapper you wrote that didn't cause the compiler to freak out. I'm simply trying to play with property wrappers and some working code would be a good starting point since the compiler doesn't love me today.

If you‘re also using SwiftUI expect issues due to name collision of Color.

I wasn't going to use this with SwiftUI, I simply wanted to experiment with property wrappers.

I love the examples in the proposal and wonder if they're going to find their way into the Swift standard library.

I don‘t think any property wrapper tapes will make into 5.1 but in the future stdlib likely will introduce a set of wrappers.

Hey @Patrick_Gili

I've just released a new Kit named ValidatedPropertyKit which shows a different more advanced example of how to use and implement a Property Wrapper. Hope this helps :v:

1 Like

Not sure if you intended to ping me or the author of this thread. I can clearly say that I know property wrappers in out as I'm like the loudest person in the related threads. ;)

Wrong mention my fault :smiley:

1 Like