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)
'CGFloat' is not convertible to 'Double' error in Swift (iOS)
In swift there is no implicit conversion of numeric data types, and you are mixing integers and floats.
You have to explicitly convert the indexes to CGFloat
s:
var intWidth = ( CGFloat(i) * (sizeOfImage.width/3.0))
var fltHeight = ( CGFloat(j) * (sizeOfImage.height/3.0))
As often happening in swift, the error message is misleading - it says:
'CGFloat' is not convertible to 'Double'
whereas I'd expect:
'CGFloat' is not convertible to 'Int'
Understanding the rules for conversion in swift
4
is not a "value". It is a literal. So it can be interpreted however Swift likes. There is nothing to "convert"; it is used to create the value, but the created value will be a Float, because that is what you asked for.
But now try this:
let i : Int = 4
let mynum: Float = i
Ooooops.
To put it another way:
let mynum: Float = 4
is interpreted as
let mynum = Float(4) // we are _creating_ the value
But if we start with i
, you have to create the value:
let i : Int = 4
let mynum: Float = Float(i)
Swift won't do that for you.
BONUS LESSON Hold my beer and watch this:
struct Dog : IntegerLiteralConvertible {
var number : Int?
init(integerLiteral: Int) {
self.number = integerLiteral
}
}
let dog : Dog = 42
Float works like that, sort of.
Float' is not convertible to 'UInt8' in Swift
The error message is misleading but the problem is that Swift doesn't do implicit type conversions — just to pull one part of your expression out:
r / n
r
is a floating point type. n
is an integer type. In C the promotion rules would implicitly concert n
to a float. That doesn't happen in Swift.
You probably want explicitly to cast all your integer types to Float
to resolve the issue.
Swift numerics and CGFloat (CGPoint, CGRect, etc.)
I wrote a library that handles operator overloading to allow interaction between Int, CGFloat and Double.
https://github.com/seivan/ScalarArithmetic
As of Beta 5, here's a list of things that you currently can't do with vanilla Swift.
https://github.com/seivan/ScalarArithmetic#sample
I suggest running the test suite with and without ScalarArithmetic just to see what's going on.
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()
}
how to convert Int32 value to CGFloat in swift?
To convert between numerical data types create a new instance of the target type, passing the source value as parameter. So to convert an Int32
to a CGFloat
:
let int: Int32 = 10
let cgfloat = CGFloat(int)
In your case you can either do:
let width = CGFloat(CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).width)
let height = CGFloat(CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).height)
myLayer?.frame = CGRectMake(0, 0, width, height)
or:
let width = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).width
let height = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).height
myLayer?.frame = CGRectMake(0, 0, CGFloat(width), CGFloat(height))
Note that there is no implicit or explicit type casting between numeric types in swift, so you have to use the same pattern also for converting a Int
to Int32
or to UInt
etc.
Ambiguous without more context error in Swift
You seem to declare the function as accepting
(attribute: NSLayoutAttribute, offset: CGFloat)
But you pass
(attribute: NSLayoutAttribute, offset: int)
Try
[(NSLayoutAttribute.centerX, CGFloat(0)), (NSLayoutAttribute.centerY, CGFloat(30))]
Int is not directly convertible to float
Related Topics
Swift Encode Tuple Using Nscoding
How Are Int and String Accepted as Anyhashable
How to Open a Screen Directly in Xcuitest
Map and Flatmap Difference in Optional Unwrapping in Swift 1.2
Safe to Signal Semaphore Before Deinitialization Just in Case
Swift Realm Property '*' Has Been Added to Latest Object Model Migration
Xcode 6, Swift - Read Standard Input (Console) to String
Play Local Audio File with Avaudioplayer
Why Is the Swift Compiler Marking This as an Error
How to Make Cells Have a Gradient Background
Realitykit - How to Set a Modelentity's Transparency
How to Generate Random Numbers Without Repetition in Swift
Retrieve Multiple Photos Under a Node from Firebase Storage
What Is the Replacement for Isdigit() for Characters in Swift