微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将Int32值转换为CGFloat?

这里我的代码

let width = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!)。width – return Int32

let height = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!)。height – return Int32

myLayer?.frame = CGRectMake(0,0,width,height)

错误是’Int32’不能转换为CGFloat,如何将Int32转换为CGFloat?

要在数值数据类型之间进行转换,将创建一个新的目标类型实例,将源值作为参数传递。所以要将Int32转换为CGFloat:
let int: Int32 = 10
let cgfloat = CGFloat(int)

在你的情况下,你可以做:

let width = CGFloat(CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).width)
let height = CGFloat(CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).height)

myLayer?.frame = CGRectMake(0,width,height)

要么:

let width = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).width
let height = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).height

myLayer?.frame = CGRectMake(0,CGFloat(width),CGFloat(height))

请注意,swift中的数字类型之间没有隐式或显式类型转换,所以您必须使用相同的模式,也可以将Int转换为Int32或UInt等。

原文地址:https://www.jb51.cc/swift/320415.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐