小编Sha*_*tel的帖子

将字符串转换为整数iOS + Swift 2.0

我正在访问天气API,当我解析JSON时,结果是一个字符串.因为,我访问的值是一个数字,我想将其转换为整数,但是当字符串具有十进制值时,该字符串不会转换为int.我的代码:

let UVIndex = swiftyJSON["current_observation"]["UV"].stringValue
let cityName = swiftyJSON["current_observation"]["display_location"]["full"].stringValue
if let UVIndexInt = Int(UVIndex) {
    print("Successfully converted! \(UVIndexInt)")
    self.UVIndexString = String(stringInterpolationSegment: UVIndex)
}
dispatch_async(dispatch_get_main_queue()) {
    self.UVIndexButton.setTitle(self.UVIndexString, forState: UIControlState.Normal)
    self.cityName.text = cityName
}
Run Code Online (Sandbox Code Playgroud)

int ios swift swift2

1
推荐指数
1
解决办法
285
查看次数

标签 统计

int ×1

ios ×1

swift ×1

swift2 ×1