@propertyWrapper for a base64 encoded string

I'm trying something basic with propertyWrapper - a base64 encoded string - and this code is causing Xcode to have a minor meltdown in that it won't compile.

@propertyWrapper
struct Base64EncodedString {
    private(set) var value: String = ""
    
    var wrappedValue: String {
        get { value }
        set { value = Data(newValue.utf8).base64EncodedString() }
    }
    
    init(initialValue: String) {
        self.wrappedValue = initialValue
    }
}

@Base64EncodedString var myToken: String = "a:a"

print(myToken)

Any advice?

Is this part of your code part of a type or is this a local variable? Local variables are not supported to have a property wrapper yet.

struct Test {
  @Base64EncodedString var myToken: String = "a:a"
}

let test = Test()
print(test.myToken)

Compiles and prints YTph just fine for me.

1 Like

Thanks @DevAndArtist . Putting it inside a struct or class solved it. I was testing in a Playground.

Your code will eventually work, just not in Swift 5.1. Property wrapper support for local variables won't make it in the release but will definitely be implemented in the future.

1 Like

Can the compiler output an error stating "Local variable propertyWrapper is not supported now"?

Right now Xcode just freeze for a long time, then crash...in playground

Xcode is still behind the implementation on master. I can‘t answer that question for sure, I would guess that this will be the expected behavior, but I do know that Douglas recently added diagnostics for top level code which is also currently not supported.