TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Escaping closure captures mutating 'self' parameter (I really need help!)

Forums > SwiftUI

Problem is in circle fuction.

import Foundation
import SwiftUI

struct Node: Decodable, Hashable {
    public let id: Int
    public var x: Double
    public var y: Double
    public let text: String
    public var red: Double
    public var green: Double
    public var blue: Double
    public var shape: Int
}

extension Node {
    mutating func circle() -> some View {
        Circle()
            .fill(Color(red: red, green: green, blue: blue))
            .frame(width: 64, height: 64, alignment: .center)
            .position(x: x, y: y)
            .gesture(DragGesture()
                .onChanged { value in
                    self.x = value.translation.width
                    self.y = value.translation.height
                }
                .onEnded { value in
                    self.x = 0
                    self.y = 0
                }
            )
    }

    func square() -> some View {
        Rectangle()
            .fill(Color(red: red, green: green, blue: blue))
            .frame(width: 64, height: 64, alignment: .center)
            .position(x: x, y: y)
    }

    @ViewBuilder mutating func drawShape() -> some View {
        switch shape{
        case 1: circle()
        case 2: square()
            default: Text("Failed")
        }
    }

    func label() -> some View {
        return Text(text)
            .font(.system(size: 30))
            .bold()
            .position(x: x, y: y)
            .foregroundColor(Color(red: 0.25, green: 0.25, blue: 0.25))
    }
}

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.