简体   繁体   English

UInt8在iOS Swift中无法转换为CGFloat错误

[英]UInt8 is not convertible to CGFloat error in iOS Swift

I am a beginner level programmer in iOS app development using Swift. 我是使用Swift进行iOS应用开发的初级程序员。 Now I am facing the compile time issue "UInt8 is not convertible to CGFloat" 现在,我遇到了编译时问题“ UInt8无法转换为CGFloat”

var numberOfAvatars:Int = 8
let count:Int = 1
let columns:Int = 3
let dimension:CGFloat = 84.0
var spacing:CGFloat = (avatarContentcroll.frame.size.width - columns * dimension)/(columns+1)
var scHeight:CGFloat = spacing + (numberOfAvatars/columns) * (dimension + spacing)

I've tried all the solutions out there and did many experiments. 我已经尝试了所有解决方案,并进行了许多实验。 And I am not sure why still I am getting this error. 而且我不确定为什么仍然出现此错误。

You have to do it explicitly as 您必须明确地这样做

var spacing:CGFloat = CGFloat((avatarContentcroll.frame.size.width) - CGFloat(columns) * (dimension))/(columns+1)
var scHeight:CGFloat = CGFloat(spacing + (CGFloat(numberOfAvatars)/CGFloat(columns)) * (dimension + spacing))

or you can try converting every expression in CGFloat 或者您可以尝试转换CGFloat中的每个表达式

var spacing:CGFloat = ((avatarContentcroll.frame.size.width) - CGFloat(columns) * (dimension))/(columns+1)
var scHeight:CGFloat = spacing + (CGFloat(numberOfAvatars)/CGFloat(columns)) * (dimension + spacing)

You must explicitly cast your Int s as CGFloat s (via as ), or construct them -- like so: 您必须将Int显式转换为CGFloat (通过as ),或构造它们-像这样:

var spacing:CGFloat = ((avatarContentcroll.frame.size.width) - CGFloat(columns) * (dimension))/(columns+1)
var scHeight:CGFloat = spacing + (CGFloat(numberOfAvatars)/CGFloat(columns)) * (dimension + spacing)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM