简体   繁体   中英

UInt8 is not convertible to CGFloat error in iOS Swift

I am a beginner level programmer in iOS app development using Swift. Now I am facing the compile time issue "UInt8 is not convertible to 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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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