Property wrapper accessing enclosing class

I have the following property wrapper:

@propertyWrapper
public struct Parameter {

    var parameter: AUParameter?

    public var wrappedValue: AUValue {
        get { parameter?.value ?? 0.0 }

       set {
            parameter?.value = newValue
       }
   }

    public var projectedValue: AUParameter {
      return parameter!
    }

    public init() { }
}

This might be used in an object like so:

class Oscillator : Node {
   @Parameter var frequency: AUValue

   init() {
      _frequency.parameter = self.getParameterByName("frequency")
   }
}

What I'd like to do is have:

class Oscillator : Node {
   @Parameter("frequency") var frequency: AUValue
}

And not need to look the parameter up by name in the init method.

Is this doable?

Add the key / name of the parameter to the Parameter initializer
public init(_ key: String) { self.parameter = getParameterByName(key) }

getParameterByName isn't at global scope ... it's on the Node class. I added self. to make that clear.

Terms of Service

Privacy Policy

Cookie Policy