Questions tagged [cgfloat]

A CGFloat is the basic CoreGraphics type for all floating-point values in the Cocoa and Cocoa Touch UI frameworks.

A CGFloat is the basic CoreGraphics type for all floating-point values in the Cocoa and Cocoa Touch UI frameworks.

CGFloat is defined in the CGGeometry Reference of CoreGraphics, which defines structures for geometric primitives and functions that operate on them. Depending on your platform, a CGFloat is either 32 or 64 bits.

Use this tag when asking questions on CGFloat value types themselves, or CoreGraphics functions which use them.

207 questions
103
votes
3 answers

Casting CGFloat to Float in Swift

I need to store a value as a Float, but the source data is a CGFloat: let myFloat : Float = myRect.origin.x but this results in the compiler error: 'NSNumber' is not of subtype 'Float' So if I explicitly cast it like this: let myFloat : Float =…
Andrew Ebling
  • 10,175
  • 10
  • 58
  • 75
102
votes
7 answers

Round up a CGFloat in Swift

How can I round up a CGFloat in Swift? I've tried ceil(CDouble(myCGFloat)) but that only works on iPad Air & iPhone 5S. When running on another simulated device I get an error saying 'NSNumber' is not a subtype of 'CGFloat'
Oskar Persson
  • 6,605
  • 15
  • 63
  • 124
88
votes
11 answers

Swift random float between 0 and 1

In Swift, I'm trying to get a random float between 0 and 1 but I can't seem to get the type conversions to work. func randomCGFloat() -> CGFloat { return CGFloat(arc4random()) / UINT32_MAX } I'm getting a 'CGFloat' is not convertible to 'UInt8'…
Joe_Schmoe
  • 1,485
  • 3
  • 17
  • 19
66
votes
8 answers

Is there a constant for the maximum CGFloat value?

I need to create a CGSize to compute text height of an arbitrary text with arbitrary length. UIKit has this nice method -sizeWithFont:constrainedToSize: and my text is only constrained in width, but not in height. For this, I need to set the maximum…
Proud Member
  • 40,078
  • 47
  • 146
  • 231
53
votes
1 answer

How to check for NaN value in Objective-C (iOS)

Possible Duplicates: Objective-C - float checking for nan Determine if NSNumber is NaN I have a problem with NaN values in CGFloat, how can I check if the number is valid? the only way so far that works is: if ([[NSString stringWithFormat:@"%f",…
Ondrej Rafaj
  • 4,342
  • 8
  • 42
  • 65
36
votes
3 answers

Converting CGFloat to String in Swift

This is my current way of converting a CGFloat to String in Swift: let x:Float = Float(CGFloat) let y:Int = Int(x) let z:String = String(y) Is there a more efficient way of doing this?
iamktothed
  • 1,398
  • 2
  • 14
  • 22
34
votes
5 answers

Convert CGFloat to NSNumber in Swift

I have an extension in Swift that holds some properties which are CGFloat's. The problem is that I don't know how to get the store the CGFloat value as a NSNumber using the associated objects Here is the code I have that doesn't work but it details…
FireDragonMule
  • 1,347
  • 2
  • 16
  • 27
32
votes
2 answers

Use of undeclared type 'UIImage' in Swift

On using this function in swift and i am getting compiler error. Function is: class func imageWithImage (imageToResize : UIImage, scaledToSize newSize : CGSize) { return imageToResize; } and the error are: Use of undeclared type 'UIImage' Use…
Gaurav Pandey
  • 1,953
  • 3
  • 23
  • 37
28
votes
4 answers

Swift Double is Not Convertible to CGFloat

I'm trying to draw a simple circle when I get to the following line I get the error "Double is Not Convertable to CGFloat under the startAngle = 0.0 path.addArcWithCenter(center, radius: radius, startAngle: 0.0, endAngle: Float(M_PI) * 2.0,…
dcbenji
  • 4,598
  • 5
  • 21
  • 23
27
votes
2 answers

how to convert Int32 value to CGFloat in swift?

Here my code. I am passing two values into CGRectMake(..) and getting and error. let width = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).width // return Int32 value let height =…
iosLearner
  • 1,312
  • 1
  • 16
  • 30
26
votes
3 answers

Binary operator '+' cannot be applied to two CGFloat operands?

Coding in Swift and get the above error... Is the message masking something else OR can you really not add two CGFloat operands? If not, why (on earth) not? EDIT There is nothing special in the code I am trying to do; what is interesting is that the…
Sam
  • 2,745
  • 3
  • 20
  • 42
23
votes
6 answers

How to round CGFloat

I made this method + (CGFloat) round: (CGFloat)f { int a = f; CGFloat b = a; return b; } It works as expected but it only rounds down. And if it's a negative number it still rounds down. This was just a quick method I made, it isn't…
Johannes Jensen
  • 1,228
  • 2
  • 18
  • 28
20
votes
4 answers

How can use CGFloat in Swift?

var posinonY:Float = Float(y) + Float(pipeDown.size.height) + Float(verticalPipeGap) pipeDown.position = CGPointMake(0.0, Float(posinonY)) I get this error: "NSNumber' is not a subtype of 'CGFloat'" Why? Edit: CGFLoat need double…
Aeli.li
  • 221
  • 1
  • 2
  • 6
18
votes
2 answers

How to set a CGFloat to a UILabel's text property

I have a UILabel called optionsDisplayerBrushSizeLabel who's text property I want to set to a CGFloat: brushSizeVal. I understand that the text property needs to be a string but I don't know how to convert a CGFloat to an NSString.
Jab
  • 26,853
  • 21
  • 75
  • 114
17
votes
2 answers

How do I make an array of CGFloats in objective c?

So I'm working on a simple iPhone game and am trying to make a local high score table. I want to make an array and push the highest scores into it. Below is the code I have so far: CGFloat score; score=delegate.score; NSInteger…
meman32
  • 766
  • 2
  • 6
  • 9
1
2 3
13 14