I have tried on Xcode 14 beta 5, beta 6 and recent RC1
All of them fail to compile. Xcode 13 works without any problems.
Is the property wrapper syntax changed on Swift 5.7?
struct SomeStructure {
@SmallNumber var someNumber: Int
}
func someFunction() {
@SmallNumber var myNumber: Int = 0
var someStructure = SomeStructure()
someStructure.someNumber = 4
// Error: Value of type 'SomeStructure' has no member '$someNumber'
print(someStructure.$someNumber)
}
complete codes here
The code you shared fails to compile in Xcode 13.4.1 as well as Xcode 14 for me, which is what I expected.
The $ prefixed property for your property wrapper returns the value that you have as your property wrapper's projectedValue property. If you don't define a projectedValue, no $ prefixed property is generated.
For example, redefining your property wrapper as shown below allows access with $
@propertyWrapper
struct SmallNumber {
private var maximum: Int
private var number: Int
var wrappedValue: Int {
get { return number }
set { number = min(newValue, maximum) }
}
var projectedValue: String {
return "the number is \(number)"
}
init() {
maximum = 12
number = 0
}
init(wrappedValue: Int) {
maximum = 12
number = min(wrappedValue, maximum)
}
init(wrappedValue: Int, maximum: Int) {
self.maximum = maximum
number = min(wrappedValue, maximum)
}
}
The code you posted still doesn't compile with that change due to how things are set up in the function; looks like defining a property-wrapped property in a function isn't allowed.
1 Like
MPLewis
(Mike Lewis)
3
This, exactly.
If you're looking to reference the underlying property wrapper itself, you can use the underscore-prefixed property, _someNumber.
hborla
(Holly Borla)
4
This is correct. However, there was a narrower issue where property wrapper projections and backing storage variables could not be referenced with $ and _, respectively, specifically for local property wrappers inside closures: Property Wrapper cannot be compiled correctly with `$` in function body (Xcode 14 and main?) · Issue #59295 · apple/swift · GitHub
I don't believe this was fixed in time for Xcode 14 RC. You can work around this by using a local function instead of a closure, pulling the local wrapped property outside of the closure, using a local struct that stores the property wrapper inside the closure, etc.
3 Likes
Surely a property wrapper is for properties, which are defined as members of types such as protocols, classes, structs or enums.
A var within a function is just that - a variable. It is not a property. So I would not expect property wrappers to work there.
xwu
(Xiaodi Wu)
6
This doesn't follow. Property wrappers are for properties, but this does not mean they are exclusively for properties.
I just bought a bathmat recently, and it works perfectly for absorbing water after I take a shower, even though a shower is not a bath. Sometimes I put other electronics or even sheets of paper in my laptop bag, and it holds them just fine even though they are not laptops.
1 Like