cgfloat

Convert CGFloat to NSNumber in Swift

妖精的绣舞 提交于 2019-12-08 14:28:00
问题 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 what I want to do: var scaledFontSize: CGFloat { get { guard let fontSize = objc_getAssociatedObject(self, &AssociatedKeys.scaledFontSize) as? NSNumber else { //Set it let scaledFont:CGFloat = VGSizeValues.getValueFromValue(self.font.pointSize); /

Accessing global const CGFloat defined in an Objective-c .m file from Swift

倾然丶 夕夏残阳落幕 提交于 2019-12-07 02:51:50
问题 I have defined some constants in my .m files that I need to access form my swift code. They are defined: const CGFloat testValue = 40.0; and in my other objective-c .m files I can access them by using extern : extern const CGFloat testValue Is there an equivalent way of making these constants accessible from the .swift files? 回答1: Add the extern to your bridging header and Swift should be able to access it. This simple test worked for me: ObjCTest.m #import <Foundation/Foundation.h> const

How can use CGFloat in Swift?

强颜欢笑 提交于 2019-12-06 17:04:27
问题 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 type pipeDown.position = CGPointMake(0.0, Double(posinonY)) this is ok. 回答1: Use CGFloat(number) instead of Float(number) pipeDown.position = CGPointMake(0.0, CGFloat(posinonY)) 回答2: Since CGFloat is not a Double on 32bit, it becomes hard to use CGGeometry and

Is there a performance diff using CGFloat with or without postfix .f in Objective-C

核能气质少年 提交于 2019-12-06 03:17:55
问题 Should I be writing CGFloat values with postfix f or not? CGFloat fValue = 1.2; vs. CGFloat fValue = 1.2f; I know that this postfix define a float value. But is it necessary, does it make sense, are there any performance differences between using those two or is this just visual presentation so you can quickly define value type (e.g. float in this case)? 回答1: 1.2 is a double ; i.e. 64-bit double-precision floating point number. 1.2f is a float ; i.e. 32-bit single-precision floating point

NSNumber from CGFloat

旧街凉风 提交于 2019-12-05 17:49:48
问题 Is there a safe way to "convert" a CGFloat to a NSNumber ? NSNumber has the numberWithFloat: and numberWithDouble: methods but CGFloat being defined as float or double depending on the platform, it seems risky to use either one of them. Or is numberWithDouble: safe to use with a CGFloat , as it is the one with the more precision ? 回答1: I believe @ NSNumber literal is good enough @(myCGFloat) Read more here: http://clang.llvm.org/docs/ObjectiveCLiterals.html 回答2: This is how I handled it:

Cast CGFloat to Int in extension BinaryFloatingPoint

本小妞迷上赌 提交于 2019-12-05 15:58:14
FIXED IN SWIFT 4.2 Details Xcode 9.2, Swift 4 Code extension BinaryFloatingPoint { func toInt() -> Int { // Eror if remove this block (I do not know why) // if let value = self as? CGFloat { // return Int(value) // } return Int(self) } } Error occurrence let float: Float = 2.38945 print("Float: \(float.toInt())") // No Error let double = Double(float) print("Double: \(double.toInt())") // No Error let cgfloat = CGFloat(float) print("CGFloat(): \(Int(cgfloat))") // No Error print(".toInt(): \(cgfloat.toInt())") // Thread 1: Fatal error: Conversion is not supported Question Why Int(cgfloat) //

Accessing global const CGFloat defined in an Objective-c .m file from Swift

房东的猫 提交于 2019-12-05 07:43:53
I have defined some constants in my .m files that I need to access form my swift code. They are defined: const CGFloat testValue = 40.0; and in my other objective-c .m files I can access them by using extern : extern const CGFloat testValue Is there an equivalent way of making these constants accessible from the .swift files? Add the extern to your bridging header and Swift should be able to access it. This simple test worked for me: ObjCTest.m #import <Foundation/Foundation.h> const CGFloat testValue = 40.0; ObjCSwiftBridgeTest-Bridging-Header.h #import <Foundation/Foundation.h> extern const

How can use CGFloat in Swift?

房东的猫 提交于 2019-12-04 22:42:30
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 type pipeDown.position = CGPointMake(0.0, Double(posinonY)) this is ok. Use CGFloat(number) instead of Float(number) pipeDown.position = CGPointMake(0.0, CGFloat(posinonY)) Seivan Since CGFloat is not a Double on 32bit, it becomes hard to use CGGeometry and frameworks like CoreGraphics or SpriteKit. This library makes it a little easier and hopefully Apple takes care

Is there a performance diff using CGFloat with or without postfix .f in Objective-C

老子叫甜甜 提交于 2019-12-04 07:44:01
Should I be writing CGFloat values with postfix f or not? CGFloat fValue = 1.2; vs. CGFloat fValue = 1.2f; I know that this postfix define a float value. But is it necessary, does it make sense, are there any performance differences between using those two or is this just visual presentation so you can quickly define value type (e.g. float in this case)? 1.2 is a double ; i.e. 64-bit double-precision floating point number. 1.2f is a float ; i.e. 32-bit single-precision floating point number. In terms of performance, it doesn't matter as the compiler will convert literals from float to double

Replacement for `fabs`, `fmax`, etc. for use with CGFloat on 64-bit iOS devices

一笑奈何 提交于 2019-12-04 02:41:30
Question Consider layout code like this: CGFloat descriptionHeight = // height of a paragraph of text with zero or more words; CGFloat imageHeight = // height of an image; CGFloat yCoordinateOfSomeOtherView = fmax(descriptionHeight, imageHeight) + 5.0f; How should the third line be rewritten with support for 64-bit iOS devices? (The current code doesn't take into account whether yCoordinateOfSomeOtherView is a float or a double .) Options A few options (I'm not sure which is best): 1. Define my own macro #if defined(__LP64__) && __LP64__ #define cgfmax(x,y) fmaxf(x,y) #else #define cgfmax(x,y)