Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何解析像“quot;14px“;在Swift中获取Int值14_Swift - Fatal编程技术网

如何解析像“quot;14px“;在Swift中获取Int值14

如何解析像“quot;14px“;在Swift中获取Int值14,swift,Swift,我从服务器API收到一个类似“14px”的字符串。如何通过忽略子字符串“px”将该字符串转换为值为14的Int?如果字符串总是只包含px,最后您可以将其下标并忽略最后2个字符 let str = "14px" var numStr = str.substring(to: str.index(name.endIndex, offsetBy: -2)) print(numStr) // "14" //Now you can convert "14" to Int print(Int(numStr

我从服务器API收到一个类似“14px”的字符串。如何通过忽略子字符串“px”将该字符串转换为值为14的Int?

如果字符串总是只包含
px
,最后您可以将其下标并忽略最后2个字符

let str = "14px"  
var numStr = str.substring(to: str.index(name.endIndex, offsetBy: -2))
print(numStr) // "14"
//Now you can convert "14" to Int
print(Int(numStr))

print(Int(String(str.characters.dropLast(2))))
通过去掉“px”或最后两个字符-问题出在哪里?