cgfloat

Converting CGFloat to String in Swift

試著忘記壹切 提交于 2019-12-03 10:45:20
问题 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? 回答1: 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.

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

帅比萌擦擦* 提交于 2019-12-03 09:38:25
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 above error message, VERBATIM, is what the Swift assistant compiler tells me (underlining my code with red squiggly lines). Running Xcode 6.3 (Swift 1.2) It's absolutely possible, adding two CGFloat variables using the binary operator '+'. What you need to know is the resultant variable is also a CGFloat variable (based on type Inference Principle)

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

北城余情 提交于 2019-12-03 03:27:38
问题 This question already has answers here : Closed 8 years ago . 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", output] isEqualToString:@"nan"]) { output = 0; } which is not a nice solution at all! :) ... and I am pretty sure that there is something else I should do instead. 回答1: There is a define

Converting CGFloat to String in Swift

て烟熏妆下的殇ゞ 提交于 2019-12-03 01:17:29
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? 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. The fast way: let x = CGFloat(12.345) let s = String(format: "%.3f", Double(x)) The better way, because it

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

百般思念 提交于 2019-12-02 16:57:34
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", output] isEqualToString:@"nan"]) { output = 0; } which is not a nice solution at all! :) ... and I am pretty sure that there is something else I should do instead. 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

Swift function that takes in array giving error: '@lvalue $T24' is not identical to 'CGFloat'

泪湿孤枕 提交于 2019-12-02 08:26:57
问题 So I'm writing a lowpass accelerometer function to moderate the jitters of the accelerometer. I have a CGFloat array to represent the data and i want to damp it with this function: // Damps the gittery motion with a lowpass filter. func lowPass(vector:[CGFloat]) -> [CGFloat] { let blend:CGFloat = 0.2 // Smoothens out the data input. vector[0] = vector[0] * blend + lastVector[0] * (1 - blend) vector[1] = vector[1] * blend + lastVector[1] * (1 - blend) vector[2] = vector[2] * blend + lastVector

Using iPhone/Objective C CGFloats: Is 0.1 okay, or should it be 0.1f?

[亡魂溺海] 提交于 2019-12-02 06:28:41
When using an iPhone Objective C method that accepts CGFloat s, e.g. [UIColor colorWithRed:green:blue:] , is it important to append a f to constant arguments to specifiy them explicitly as floats, e.g. should I always type 0.1f rather than 0.1 in such cases? Or does the compiler automatically cast 0.1 (which is a double in general) to 0.1f (which is a float) at compile time? I don't wish to have these casts happen at run time because they would unneccessarily hog performance. Thanks in advance MrMage It's not important ; it won't break anything to use a double-precision constant where a single

“'CGFloat' is not convertible to 'Double'” error in Swift (iOS)

我们两清 提交于 2019-12-02 04:17:27
问题 I'm trying to cut an image into 9 pieces in Swift. I'm getting this error: 'CGFloat' is not convertible to 'Double' I get this error when I put i or j in the two variables. Below is part of the code used for cutting the image. for i in 1...3 { for j in 1...3 { var intWidth = ( i * (sizeOfImage.width/3.0)) var fltHeight = ( j * (sizeOfImage.height/3.0)) var portion = CGRectMake(intWidth,fltHeight, sizeOfImage.width/3.0, sizeOfImage.height/3.0); . . "Code goes on" What is the problem? 回答1: In

Binary Operator + Cannot be Applied to Operands of Type CGfloat int

前提是你 提交于 2019-12-01 21:00:14
I am having the same problem as earlier with a different line of code; but this time, I wasn't able to fix it with the same approach as last time: var Y : Int = 0 var X : Int = 0 @IBOutlet var ball : UIImageView! ball.center = CGPointMake(ball.center.x + X, ball.center.y + Y) This is the error I am getting: binary operator + cannot be applied to operands of type CGfloat int Declare them, instead, as the following: let X : CGFloat = 0.0 let Y : CGFloat = 0.0 Replying to your comment: The error has nothing to do with them being declared as var or let . You could declare them as var and if you so

Why use Float(arc4random()) / 0xFFFFFFFF instead of drand()

我的未来我决定 提交于 2019-12-01 05:38:52
问题 I'm new to Swift and just saw this code used to generate a random angle degree in a tutorial. func random() ->CGFloat{ return CGFloat(Float(arc4random()) / 0xFFFFFFFF) } func random(#min: CGFloat, max:CGFloat) ->CGFloat{ return random()*(max-min)+min } I'm wondering is the line return CGFloat(Float(arc4random()) / 0xFFFFFFFF) generates a random float number between 0 and 1.0? Then why cannot just use drand()? Any difference between the two functions? Thanks! 回答1: drand48() is fine for lots of