cgfloat
Convert CGFloat to NSNumber in Swift
In Swift 3.0 let myFloat : CGFloat = 1234.5 let myNumber = NSNumber(value: Float(myFloat)) or let myNumber = NSNumber(value: Double(myFloat)) In Swift 2 let myNumber = NSNumber(double: myFloat.native) or let myNumber = NSNumber(double: Double(myFloat)) or let myNumber = NSNumber(float: Float(myFloat))
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 check for NaN value in Objective-C (iOS) [duplicate]
There is a define for checking if a number is nan inf etc in math.h (you can use it without import I think). isnan(myValue) if you follow the define you will end up with (x!=x) there are also some other useful defines like isinf, isnormal , isfinite , …
Is there a constant for the maximum CGFloat value?
For those using Swift 2, you should use: CGFloat.max For those using Swift 3, you should use: CGFloat.greatestFiniteMagnitude Note that CGFloat.max was removed when Swift 3 came out, as reflected in the documentation.
Swift random float between 0 and 1
This is extension for random numbers of Int, Double, Float, CGFloat Swift 3 & 4 & 5 syntax import Foundation import CoreGraphics // MARK: Int Extension public extension Int { /// Returns a random Int point number between 0 and Int.max. static var random: Int { return Int.random(n: Int.max) } /// Random integer between 0 … Read more
Round up a CGFloat in Swift
Update: Apple have now defined some CGFloat-specific versions of common functions like ceil: func ceil(x: CGFloat) -> CGFloat …specifically to cope with the 32/64-bit difference. If you simply use ceil with a CGFloat argument it should now work on all architectures. My original answer: This is pretty horrible, I think, but can anyone think of … Read more
Casting CGFloat to Float in Swift
You can use the Float() initializer: let cgFloat: CGFloat = 3.14159 let someFloat = Float(cgFloat)