Convert String to CGFloat in Swift
If you want a safe way to do this, here is a possibility:
let str = "32.4"
if let n = NumberFormatter().number(from: str) {
let f = CGFloat(truncating: n)
}
If you change str
to "bob", it won't get converted to a float, while most of the other answers will get turned into 0.0
Side note: remember also that decimal separator might be either comma or period. You might want to specify it inside the number formatter
let formatter = NumberFormatter()
formatter.decimalSeparator = "." // or ","
// use formatter (e.g. formatter.number(from:))
How to convert Any to CGFloat in Swift 3
May be ix
and iy
is just Double
but not the CGFloat
, First try to convert it to Double
then you can easily convert Double
to CGFloat
.
let lx = CGFloat(ix as? Double ?? 0)
let ly = CGFloat(iy as? Double ?? 0)
How to convert NSTextField value to CGFloat
CGFloat
doesn't have a string initializer.
Just get the doubleValue
from the text field rather than the stringValue
let countY = CGFloat(self.textbox1.doubleValue)
if let image = CGDisplayCreateImage(CGMainDisplayID(), rect: CGRect(x: countX, y: countY, width: 1, height: 1)) {
}
Angle to Double or CGFloat or String - SwiftUI
Instances of type Angle
have degrees
property of type Double
. See the documentation.
let angle = Angle(degrees: 20)
let degrees = angle.degrees // 20 of type Double
Converting String array to CGFloat array in Swift
The easiest way is to use map
to transform each element from one type to another type:
let doubleArray = StringArray.map {
CGFloat(($0 as NSString).doubleValue)
}
The cast to NSString
is needed because there's no string to double conversion in plain swift - at least I am not aware of any.
Get first number of type CGFloat
You said:
For example if the screen width is 375.0 then display 3 columns, If somewhere around 600 then display 6 columns, if around 1000 then display 10 columns and so on with equal width.
So what you really want is not the first digit of the width (which would
for example be 1
for width = 1024
instead of the desired 10
)
but the width divided by 100 and rounded down to the next integral value:
let numColumns = (width / 100.0).rounded(.down)
Or, as an integer value:
let numColumns = Int(width / 100.0)
Converting CGFloat to String in Swift
You can use string interpolation:
let x: CGFloat = 0.1
let string = "\(x)" // "0.1"
Or technically, you can use the printable nature of CGFloat directly:
let string = x.description
The description
property comes from it implementing the Printable
protocol which is what makes string interpolation possible.
How to convert string to double in swift
Value of optional type 'Double?' not unwrapped; did you mean to use '!' or '?'?
This error appears because you are trying to add an optional value to total
.
Double(item)
produces an optional result so you can add it in if-let chain.
if let item = value as? String, let doubleVal = Double(item) {
total += doubleVal
}
Related Topics
How to Check If a String Contains Letters in Swift
Prettier Debug Output Printing Swift Dictionary in Xcode
How to Setup Viewcontroller in Playgrounds
Swift Tableview Cell Set Accessory Type
How to Convert Copaquepointer in Swift to Some Type (Cgcontext? in Particular)
Iterate an Array W/ Explicit Object Type in Swift
Confusion Regarding Overriding Class Properties in Swift
Swift: How to Open File with Associated Application
Binary Operator '==' Cannot Be Applied to Two Operands
Bitwise Operations with Cgbitmapinfo and Cgimagealphainfo
How to Avoid Using Anypublisher/Erasetoanypublisher All Over the Place
How to Record Video in Realitykit
Adding Activity Indicator to Uialertview
How to Set Title of Navigation Bar in Swift
How to Filter Events Created for the Current Date in the Realm Swift
Variable P Passed by Reference Before Being Initialized
Ios-Charts Library: X-Axis Labels Without Backing Data Not Showing