TwelveOrLess Error

I'm trying to run a playground with some standard propertyWrapper code taken directly from the Swift Guide (5.1). I've just upgraded to Xcode 11.4.1. The code is straight from the book (Properties chapter) and it used to run fine.


@propertyWrapper struct TwelveOrLess {

private var number = 0

var wrappedValue: Int {

get { return number }

set { number = min(newValue, 12)}

}

}


Now, however, I'm getting this error:

error: private initializer 'init(number:)' cannot have more restrictive access than its enclosing property wrapper type 'TwelveOrLess' (which is internal)

@propertyWrapper struct TwelveOrLess {

^

Any ideas?

Thanks.

What’s going on here is that the compiler is synthesising a default initialiser for your

TwelveOrLess
structure, but that default initialiser is private because the underlying
name
property is private. You can fix this by implementing an explicit initialiser that’s not private:
@propertyWrapper struct TwelveOrLess {
  init() {
    self.number = 0
  }
  private var number: Int
  var wrappedValue: Int {
    get { return number }
    set { number = min(newValue, 12)}
  }
}

The code is straight from the book (Properties chapter) and it used to run fine.

Indeed. I suspect that this is fallout from SE-0242 Synthesize default values for the memberwise initializer but it’s not that simple because both features landed at the same time. Regardless, please file a bug against the book, and post your bug number here, just for the record.

Share and Enjoy

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

let myEmail = "eskimo" + "1" + "@apple.com"
TwelveOrLess Error
 
 
Q