cgfloat

CGFloat: round, floor, abs, and 32/64 bit precision

眉间皱痕 提交于 2019-11-30 18:21:41
TLDR: How do I call standard floating point code in a way that compiles both 32 and 64 bit CGFloats without warnings? CGFloat is defined as either double or float, depending on the compiler settings and platform. I'm trying to write code that works well in both situations, without generating a lot of warnings. When I use functions like floor, abs, ceil, and other simple floating point operations, I get warnings about values being truncated. For example: warning: implicit conversion shortens 64-bit value into a 32-bit value I'm not concerned about correctness or loss of precision in of

What's the difference between using CGSizeMake and CGSize? Is one better than the other?

血红的双手。 提交于 2019-11-30 15:27:48
CGSize(width: 360, height: 480) and CGSizeMake(360, 480) seem to have the same effect. Is one preferred to the other? What is the difference? The CGSize constructor is a Swift extension on CGSize : extension CGSize { public static var zero: CGSize { get } public init(width: Int, height: Int) public init(width: Double, height: Double) } CGSizeMake is a leftover inline function bridged from Objective-C: /*** Definitions of inline functions. ***/ // ... public func CGSizeMake(width: CGFloat, _ height: CGFloat) -> CGSize Both have the same functionality in Swift, the CGSize constructor is just

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

旧街凉风 提交于 2019-11-30 11:12:59
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 currentindex=0; for (CGFloat *oldscore in highscores) { if (score>oldscore) { [highscores insertObject:score atIndex:currentindex] if ([highscores count]>10) { [highscores removeLastObject]; } } currentindex+=1; } The problem is that highscores is an NSMutableArray, which can only store objects. So here's my question, what is the best way to store CGFloats in an array?

CGFloat: round, floor, abs, and 32/64 bit precision

北战南征 提交于 2019-11-30 03:03:14
问题 TLDR: How do I call standard floating point code in a way that compiles both 32 and 64 bit CGFloats without warnings? CGFloat is defined as either double or float, depending on the compiler settings and platform. I'm trying to write code that works well in both situations, without generating a lot of warnings. When I use functions like floor, abs, ceil, and other simple floating point operations, I get warnings about values being truncated. For example: warning: implicit conversion shortens

What's the difference between using CGSizeMake and CGSize? Is one better than the other?

孤者浪人 提交于 2019-11-29 21:27:22
问题 CGSize(width: 360, height: 480) and CGSizeMake(360, 480) seem to have the same effect. Is one preferred to the other? What is the difference? 回答1: The CGSize constructor is a Swift extension on CGSize : extension CGSize { public static var zero: CGSize { get } public init(width: Int, height: Int) public init(width: Double, height: Double) } CGSizeMake is a leftover inline function bridged from Objective-C: /*** Definitions of inline functions. ***/ // ... public func CGSizeMake(width: CGFloat

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

丶灬走出姿态 提交于 2019-11-29 16:51:02
问题 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 currentindex=0; for (CGFloat *oldscore in highscores) { if (score>oldscore) { [highscores insertObject:score atIndex:currentindex] if ([highscores count]>10) { [highscores removeLastObject]; } } currentindex+=1; } The problem is that highscores is an NSMutableArray,

Swift enum raw value: not working with CGFloat = -1.0

試著忘記壹切 提交于 2019-11-29 16:18:59
What does this not work? enum Aspect : CGFloat { case Clockwise = 1.0 case Anticlockwise = -1.0 } On Anticlockwise line I'm told that 'raw value for enum case must be a literal' That sounds like a bug. However it seems to work if you omit the decimal part: enum Aspect : CGFloat { case Clockwise = 1 case Anticlockwise = -1 } The weird thing is that a float with a minus is not a literal, but an expression. So the error message is correct. From the Swift programming language: Unlike with integer literals, negative floating-point numbers are expressed by applying the unary minus operator (-) to a

Confusion due to Swift lacking implicit conversion of CGFloat

放肆的年华 提交于 2019-11-29 02:02:34
Trying to do arithmetic in a function that returns `CGFloat, I get an error: Couldn't find overload for '/' that accepts supplied arguments func kDCControlDegreesToRadians(x : CGFloat) -> CGFloat { return (M_PI * (x) / 180.0) // error is here. } Has anyone else seen this type of issue? This is a problem with double to float conversion. On a 64-bit machine, CGFloat is defined as double and you will compile it without problems because M_PI and x are both doubles. On a 32-bit machine, CGFloat is a float but M_PI is still a double. Unfortunately, there are no implicit casts in Swift, so you have

Swift expression was too complex to be solved in reasonable time

社会主义新天地 提交于 2019-11-28 13:32:26
I'm having an error when compiling a project in Xcode, it says: Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions here's the code: static func random(min: CGFloat, max: CGFloat) -> CGFloat { return CGFloat(Float(arc4random()/0xFFFFFFFF) * (max - min) + min) } Why not reduce the complexity for the compiler by breaking the expression down into two sub-expressions? static func random(min: CGFloat, max: CGFloat) -> CGFloat { let rand = CGFloat(arc4random()/0xFFFFFFFF) return (rand * (max - min) + min) } You can also use

How to round CGFloat

◇◆丶佛笑我妖孽 提交于 2019-11-27 21:17:18
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 very important that it rounds correctly, I just made it to round the camera's x and y values for my game. Is this method okay? Is it fast? Or is there a better solution? SergGr There are already standard functions with behaviors you might need in <math.h> such as: floorf , ceilf , roundf , rintf and nearbyintf (lasf 'f' means "float" version, versions without it