dugla dugla - 7 months ago 57
Swift Question

In Swift, how do I add a setter to immutable GLKit vector structs?

In Swift

vectors are immutable structs:

public struct _GLKVector2 {

public var v: (Float, Float)

public init(v: (Float, Float))

public init()

extension GLKVector2 {

public var x: Float { get }

public var y: Float { get }

public var s: Float { get }

public var t: Float { get }

public subscript(i: Int) -> Float { get }
public typealias GLKVector2 = _GLKVector2

I find this a bit restrictive and would like to extend
to include corresponding setters. How do I do this?


You could create a mutating func that replaces the whole self.

extension GLKVector2 {
    mutating func setX(_ x: Float) {
        self = GLKVector2Make(x, y)