Swift Compiler Error Int is not convertible to CGFloat
Pass Int
as 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 ))
Reference:
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
// ...
}
Int' is not convertible to 'CGFloat' SwiftUI Misleading error
You can't just put function call in a ViewBuilder
, because it can't resolve opaque return type.
There are several possible solutions, depending on your needs:
1) Put call of pullData
in init()
2) Provide explicit return in ViewBuilder
(all of them)
3) Call in some closure (seems most appropriate in this use-case) as in below:
struct FunAreaSmall: View {
//Code from above goes here...
var body: some View {
//Smaller UI
VStack {
HStack {
Button(action:{}) {
Image("SkipBtn")
.offset(y: 60)
.scaledToFit()
}
Button(action: {}) {
Image("IgniteBtn")
.offset(y: 60)
.scaledToFit()
}
}
}
.onAppear() {
self.pullData()
}
}
}
Int is not convertible to CGFloat error when subclassing UIImageView
In your init you forgot to call the parent initializer:
init(someParameter: Bool) {
self.someParameter = someParameter
println("\(someParameter) is being initialized")
super.init() // << this is missing
}
However it looks like there's another initializer which is required but not said explicitly:
override init(frame: CGRect) {
self.someParameter = false
super.init(frame: frame)
}
That should solve the compilation issue - what's left is figuring out how to initialize the properties implemented in your class - maybe you should use optionals.
CGFloat is not convertible to Int when trying to calculate an expression
You have already explained and solved the matter perfectly. It's simply a matter of you accepting your own excellent explanation:
It works perfectly fine if ... I just give it a literal value ... But when I go with any variable or constant or expression containing one, it displays an error "CGFloat is not convertible to Int".
Correct. Numeric literals can be automatically coerced to another type, such as CGFloat. But variables cannot; you must coerce them, explicitly. To do so, initialize a new number, of the correct type, based on the old number.
So, this works automagically:
let f1 : CGFloat = 1.0
let f2 = f1 + 1
But here, you have to coerce:
let f1 : CGFloat = 1.0
let f2 = 1
let f3 = f1 + CGFloat(f2)
It's a pain, but it keeps you honest, I guess. Personally I agree with your frustration, as my discussion here will show: Swift numerics and CGFloat (CGPoint, CGRect, etc.) It is hard to believe that a modern language is expected to work with such clumsy numeric handling, especially when we are forced against our will to bang into the Core Graphics CGFloat type so frequently in real-life Cocoa programming. But that, it seems, is the deal. I keep hoping that Swift will change in this regard, but it hasn't happened so far. And there is a legitimate counterargument that implicit coercion, which is what we were doing in Objective-C, is dangerous (and indeed, things like the coming of 64-bit have already exposed the danger).
UInt8 is not convertible to CGFloat error in iOS Swift
You have to do it explicitly as
var spacing:CGFloat = CGFloat((avatarContentcroll.frame.size.width) - CGFloat(columns) * (dimension))/(columns+1)
var scHeight:CGFloat = CGFloat(spacing + (CGFloat(numberOfAvatars)/CGFloat(columns)) * (dimension + spacing))
or you can try converting every expression in CGFloat
var spacing:CGFloat = ((avatarContentcroll.frame.size.width) - CGFloat(columns) * (dimension))/(columns+1)
var scHeight:CGFloat = spacing + (CGFloat(numberOfAvatars)/CGFloat(columns)) * (dimension + spacing)
Swift Compiler error: 'Double' is not convertible to CGFloat
You can convert it with CGFloat(M_PI)
.
For example the following code should work in your case (note the use of CGFloat
)
let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)
Swift Double is Not Convertible to CGFloat
Convert the values that need to be CGFloat to a CGFloat.
path.addArcWithCenter(center, radius: CGFloat(radius), startAngle: CGFloat(0.0), endAngle: CGFloat(M_PI) * 2.0, clockwise: true)
startAngle probably shouldn't need to be converted though if you're just passing a literal. Also note that this isn't a C style cast, but actually converting between different Swift Types.
Edit: Looking at your whole function, this works.
func drawCircle() {
// Drawing code
var bounds:CGRect = self.view.bounds
var center = CGPoint()
center.x = bounds.origin.x + bounds.size.width / 2.0
center.y = bounds.origin.y + bounds.size.height / 2.0
var radius = (min(bounds.size.width, bounds.size.height) / 2.0)
var path:UIBezierPath = UIBezierPath()
path.addArcWithCenter(center, radius: CGFloat(radius), startAngle: CGFloat(0.0), endAngle: CGFloat(Float(M_PI) * 2.0), clockwise: true)
path.stroke()
}
Related Topics
Swiftui Navigation: How to Switch Detail View to a Different Item
Format Println Output in a Table
How to Cancel Firebase Setvalue While Pending for Completion (When Offline)
Scnscene: Calculate Projected Size of an Object
Swift: How to Flush Stdout After Println
Suppress Warnings from Dependencies with Swift Package Manager
How to Trigger Xcode's 'Update to Latest Package Versions' from Command Line
Unresponsive Uibutton in Subview Added to Uistackview
Swiftui Multiline Text Background Color
How to Make Deinit Take Effect in Swift
Compound Key in Realm with Lazy Property
Distributednotificationcenter - How to Pass Data Between Applications
Numeric Types Don't Automatically Bridge to Nsnumber in Pure Swift on Ubuntu Linux
How to Build a Swift Executable for Linux on Macos
Swift Vscode Class in Other File Not Recognized
How to Convert a Computed Value to a Literal for Enum Initialization
Change Notification from Observable Object as a Nested Object in Swiftui