Swift Compiler Error Int Not Converting to CGFloat

I try to run this code, but the compiler overhears me that Int cannot convert to CGFloat, but I never declared min, max or value as 'Int' variables and never mentioned them before.

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */
    bird.physicsBody.velocity = CGVectorMake(0, 0)
    bird.physicsBody.applyImpulse(CGVectorMake(0, 8))   
}

func clamp (min: CGFloat, max: CGFloat, value:CGFloat) -> CGFloat {
    if (value > max) {
        return max
    } else if (value < min){
        return min
    }else{
        return value
    }
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */

    bird.zRotation = self.clamp(-1, max: 0.5, value: bird.physicsBody.velocity.dy * (bird.physicsBody.velocity.dy < 0 ?0.003 : 0.001 ))

}

The compiler marks "-1", the next "Int" is not converted to "CGFloat"

Please, help

+4
source share
2 answers

Pass Intas parameter to CGFloat init:

var value = -1

var newVal = CGFloat(value)  // -1.0

In your case:

bird.zRotation = self.clamp(CGFloat(-1), max: 0.5, value: bird.physicsBody.velocity.dy * (bird.physicsBody.velocity.dy < 0 ?0.003 : 0.001 ))

Link:

CGFloat:

struct CGFloat {

/// The native type used to store the CGFloat, which is Float on
/// 32-bit architectures and Double on 64-bit architectures.
typealias NativeType = Double
init()
init(_ value: Float)
init(_ value: Double)

/// The native value.
var native: NativeType
}

extension CGFloat : FloatingPointType {
    // ... 
    init(_ value: Int)  // < --- your case
    // ... 
}
+11
source

I get the same error: Int 'does not convert to' CGFloat? ' at:

struct ContentView: View {
    let posts = ["1", "2", "3", "1", "2", "3"]
    var body: some View {
    NavigationView {
        List {
            ScrollView {
                VStack (alignment: .leading){
                    Text("Trending")
                    HStack {
                        Text("Group 1")
                        Text("Group 2")
                        Text("Group 3")
                        Text("Group 4")
                        Text("Group 5")
                        Text("Group 6")

                        }
                    }
            }.frame(height: 632)

            ForEach(posts.identified(by: \.self)) { post in PostView()
            }

        }.navigationBarTitle(Text("Gastronomía Africana"))
    }
0

All Articles