Fischer
Fischer

Reputation: 1523

Writable keypath with undefined Class

I need an array of writable keypaths to edit variables, but the Class is undefined. How could I do something like this ?

var properties: [ WritableKeyPath< AnyClass, Double > ]

properties.append( \Class1.tag )
properties.append( \AnyClass2.volume )

func setPropertie (keyIndex: Int, value : Double) {
    anyObject[keyPath: properties[keyIndex] ] = value
}

In this case, trying to append( \Class1.tag ) got this error: Type 'AnyClass' (aka 'AnyObject.Type') has no member 'threshold'

Upvotes: 1

Views: 550

Answers (1)

staticVoidMan
staticVoidMan

Reputation: 20274

Your WritableKeyPath<AnyClass,Double> is made to be take AnyClass but you're putting class specific keypaths.

Class1 as AnyClass would lose access to it's tag object.
Similarly AnyClass2 as AnyClass would also lose access to it's volume object.
Hence \Class1.tag and \AnyClass2.volume won't work for a WritableKeyPath<AnyClass,Double>

What you could do instead is:

var properties = [AnyKeyPath]()
properties.append(\Class1.tag)
properties.append(\AnyClass2.volume)

Upvotes: 1

Related Questions