我需要将值存储为Float,但源数据是CGFloat:
let myFloat : Float = myRect.origin.x
Run Code Online (Sandbox Code Playgroud)
但这会导致编译器错误:'NSNumber'不属于子类型'Float'
所以如果我明确地这样做:
let myFloat : Float = myRect.origin.x as Float
Run Code Online (Sandbox Code Playgroud)
但这反过来导致编译器错误:'无法将表达式的类型'Float'转换为'Float''
这样做的正确方法是什么,请满足编译器的要求?
我怎样才能在Swift中收集CGFloat?我试过ceil(CDouble(myCGFloat))但只适用于iPad Air和iPhone 5S.
当在另一个模拟设备上运行时,我收到错误消息 'NSNumber' is not a subtype of 'CGFloat'
在Swift中,我试图获得0到1之间的随机浮点数,但我似乎无法使类型转换工作.
func randomCGFloat() -> CGFloat {
return CGFloat(arc4random()) / UINT32_MAX
}
Run Code Online (Sandbox Code Playgroud)
我得到'CGFloat'不能转换为'UInt8'错误
运行Xcode 6.
我在CGFloat中遇到NaN值问题,如何检查数字是否有效?
到目前为止唯一有效的方法是:
if ([[NSString stringWithFormat:@"%f", output] isEqualToString:@"nan"]) {
output = 0;
}
Run Code Online (Sandbox Code Playgroud)
这根本不是一个好的解决方案!:) ...而且我很确定我应该做的其他事情.
我需要创建一个CGSize来计算任意长度的任意文本的文本高度.UIKit有这个很好的方法-sizeWithFont:constrainedToSize:我的文本只受宽度约束,但不是高度.
为此,我需要为高度设置最大可能的CGFloat.
有没有像"CGFloatMax"这样的常量?
在swift中使用此函数,我收到编译器错误.功能是:
class func imageWithImage (imageToResize : UIImage, scaledToSize newSize : CGSize) {
return imageToResize;
}
Run Code Online (Sandbox Code Playgroud)
而错误是:
这有什么问题..?我怎么能在Swift中使用UIImage ..?
我在Swift中有一个扩展,它包含了一些属性CGFloat.问题是我不知道如何使用关联对象获取存储的CGFloat值NSNumber
这是我的代码不起作用,但它详细说明了我想做的事情:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法解决这个问题?
这是我目前在Swift中将CGFloat转换为String的方法:
let x:Float = Float(CGFloat)
let y:Int = Int(x)
let z:String = String(y)
Run Code Online (Sandbox Code Playgroud)
有更有效的方法吗?
在这里我的代码.我正在传递两个值CGRectMake(..)并获得和错误.
let width = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).width
// return Int32 value
let height = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).height
// return Int32 value
myLayer?.frame = CGRectMake(0, 0, width, height)
// returns error: '`Int32`' not convertible to `CGFloat`
Run Code Online (Sandbox Code Playgroud)
如何转换Int32为CGFloat不返回错误?
当我到达以下行时,我正在尝试绘制一个简单的圆圈我得到错误"Double是不可转换为CGFloat下的 startAngle = 0.0
path.addArcWithCenter(center, radius: radius, startAngle: 0.0, endAngle: Float(M_PI) * 2.0, clockwise: true)
Run Code Online (Sandbox Code Playgroud)
如何"转换"0.0以使其在Swift中成为CGFloat?
我写的完整功能:
func drawCircle() {
// Drawing code
var bounds:CGRect = secondView.bounds
var center = CGPoint()
center.x = bounds.origin.x + bounds.size.width / 2.0
center.y = bounds.origin.y + bounds.size.height / 2.0
var radius = (min(bounds.size.width, bounds.size.height) / 2.0)
var path:UIBezierPath = UIBezierPath()
path.addArcWithCenter(center, radius: radius, startAngle: CGFloat(0.0), endAngle: Float(M_PI) * 2.0, clockwise: true)
path.stroke()
}
Run Code Online (Sandbox Code Playgroud)