Convert String to CGFloat in Swift

后端 未结 12 1158
长情又很酷
长情又很酷 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:18

    Good question. There is not in fact any pure Swift API for converting a string that represents a CGFloat into a CGFloat. The only string-represented number that pure Swift lets you convert to a number is an integer. You'll have to use some other approach from some other library - for example, start with Foundation's NSString or C's (Darwin's) strtod.

提交回复
热议问题