How Can Use Cgfloat in Swift

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 a better way? #if doesn't seem to work for CGFLOAT_IS_DOUBLE; I think you're limited to build configurations, from what I can see in the documentation for conditional compilation.

var x = CGFloat(0.5)

#if arch(x86_64) || arch(arm64)
var test = ceil(x)
#else
var test = ceilf(x)
#endif

How to obtain a reference to a CGFloat using init() in Swift?

init methods are written like this in Swift:

let newFloat = CGFloat(10)

CGFloat in Swift 2.0

To elaborate on why importing UIKit fixes this problem:

The CGFloat struct is part of the CoreGraphics framework. UIKit by default imports Foundation which imports CoreGraphics.

If you weren't using UIKit or were on a platform that doesn't have the UIKit framework (such as Linux), Importing the Foundation module will import CoreGraphics.

CGFloat cannot be initialized with Float

The concept that you are apparently missing is that you did not declare a return Type for your functions.

It should be

func someFunction () -> CGFloat {
var x:Float = 5.0
return CGFloat(x)
}

This declares that someFunction is returning a value of type CGFloat.

CGFloat variable returns 0.0

If you leave out converting, this will work perfectly.

let progress: CGFloat = 2 / 3
print(progress) //0.666666666666667

The reason why this does not work with explicit converting is beause Swift treats a whole number as an Int if it's without context.

That's exactly what is happening inside converting brackets.

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)

CGFloat in Array Swift

If you're confused about how to set the CGFloat values in an init function, you could do something like this:

class myNewClass
{
var calc : [[CGFloat]] = [[77, 53, 223, 53, 77, 247, 223, 247],[63, 34, 237, 34, 63, 265, 237, 265]]

init(index:Int, value:CGFloat...)
{
calc[index] = value
}
}

If you then instantiate myNewClass with:

let newClass:myNewClass = myNewClass(index: 0, value: 1, 2, 3)

Then later say:

println(newClass.calc.count)
println(newClass.calc[0])

This would print:

2

[1.0, 2.0, 3.0]

If, during initialization, you want to add an element, you would of course use append.

Edit:

If all you want to do is associate your CGFloat arrays with particular images, you could use a dictionary and you don't really need to create a separate class to do it.

If, for example, I put the following line in my appDelegate file, but outside of (above) any class

var calcDict:[String:[CGFloat]] = ["image1.jpg": [77, 53, 223, 53, 77, 247, 223, 247], "image2.jpg":[63, 34, 237, 34, 63, 265, 237, 265]]

Because it's declared outside of any class, it's now visible from anywhere in your code.

If then, somewhere else in my code I write:

var myCGFloats = calcDict["image1.jpg"]!
println(myCGFloats)

calcDict["image3.jpg"] = [1, 2, 3]
myCGFloats = calcDict["image3.jpg"]!
println(myCGFloats)

This prints:

[77.0, 53.0, 223.0, 53.0, 77.0, 247.0, 223.0, 247.0]

[1.0, 2.0, 3.0]

The return from a dictionary lookup is an optional type, so I put an ! after each of the myCGFloats = statements to unwrap the value returned from the dictionary.

If you want to declare calcDict in your view controller class you could also do that. Inside your view controller class (let's call it MyViewController), but outside of any function (like init()) all you need to have is:

var calcDict:[String:[CGFloat]] = ["image1.jpg": [77, 53, 223, 53, 77, 247, 223, 247], "image2.jpg":[63, 34, 237, 34, 63, 265, 237, 265]]

This creates an instance variable. You could then access calcDict directly from inside your view controller, or if you have instantiated your view controller as:

myViewController = MyViewController()

then from anywhere in your code where myViewController is visible you could access calcDict as:

var myCGFloats = myViewController.calcDict["image1.jpg"]!


Related Topics



Leave a reply



Submit