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);
//Fails here
objc_setAssociatedObject(self,&AssociatedKeys.scaledFontSize, NSNumber( scaledFont),objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return scaledFont;
}
return CGFloat(fontSize.doubleValue);
}
}
Does anyone way to work around this?
For Swift 3.1
For me this is worked.
In Swift 3.0
or
In Swift 2
or
or
Swift 3
let myNumber = NSNumber(value: Float(myCGFloat))