Convert String to CGFloat in Swift

后端 未结 12 1156
长情又很酷
长情又很酷 2021-02-03 17:05

I\'m new to Swift, how can I convert a String to CGFloat?

I tried:

var fl: CGFloat = str as CGFloat
var fl: CGFloat = (CGFloat)str
var fl: CGFloat = CGFl         


        
12条回答
  •  臣服心动
    2021-02-03 17:23

    You should cast string to double and then cast from double to CGFloat, Let try this:

    let fl: CGFloat = CGFloat((str as NSString).doubleValue)
    

提交回复
热议问题