iOS-将字符串转换为双精度

罗伊

所以我正在从火力基地配置数据中获取并尝试如此操作。

这是我的结构:

struct FireBaseConfiguration: Codable {
 var updateTime: String = ""
 var iOSMinVersionForceUpdate: String = ""
 var iOSMinVersionMessageUpdate: String = ""
 var iOSMinFirmwareVersion: String = ""
 var privacyPolicyUrlFireBase: String = ""
 var termOfUseUrlFireBase: String = ""
}

这是我的解析器方法:

func fireBaseConfigVersionMapParser(dataString: String, version: String) -> FireBaseConfiguration? {

    var fireBaseConfiguration: FireBaseConfiguration?

    let data = dataString.data(using: .utf8)!

    do {
        if let jsonArray = try JSONSerialization.jsonObject(with: data, options : .allowFragments) as? NSDictionary {

           let model = jsonArray.object(forKey: version)

            let data = try JSONSerialization.data(withJSONObject: model!, options: .prettyPrinted)

            do {
             let parsedModel = try JSONDecoder().decode(FireBaseConfiguration.self, from: data)
                print("parsedModel is: \(parsedModel)")
                fireBaseConfiguration = parsedModel
            } catch let error{
                print(error)
            }
        } else {
            print("bad json")
        }

    } catch let error{
        print(error)
    }
    return fireBaseConfiguration
}

这是vc中的实现:

 self.remoteConfig?.fetch(withExpirationDuration: 0, completionHandler: { [unowned self] (status, error) in
        if status == .success {
            self.remoteConfig?.activateFetched()

            guard let configVersionMap = self.remoteConfig?.configValue(forKey: "iOSConfigVersionMap").stringValue else { return }

            if let configModel = self.parser.fireBaseConfigVersionMapParser(dataString: configVersionMap, version: self.getCurrentAppVersion()!) {

                print(configModel)
                print(configModel.iOSMinVersionForceUpdate)
                print(configModel.iOSMinVersionMessageUpdate)
                                    var doubleForceUpdate = Double(configModel.iOSMinVersionForceUpdate)
                var doubleMessageUpdate = Double(configModel.iOSMinVersionMessageUpdate)

                print(doubleForceUpdate)
                print(doubleMessageUpdate)

            }
      } else {
                  print("Config not fetched")
                  print("Error: \(error?.localizedDescription ?? "No error available.")")
        }
    })

所以印刷品是这样的:

FireBaseConfiguration(updateTime:“ 13/7/2018”,iOSMinVersionForceUpdate:“ 1.0.2”,iOSMinVersionMessageUpdate:“ 1.0.2”,iOSMinFirmwareVersion:“ 1.0.1”,privacyPolicyUrlFireBase:“ https://www.name.com/corporate / privacy-policy “,termOfUseUrlFireBase:” https://www.name.com/corporate/terms-of-use “)

1.0.2 1.0.2无

有任何想法吗?

可编写脚本

这很简单String,但实际上无效DoubleDouble值没有小数点后两位)。所以这就是为什么它失败了。

我认为您真正需要的是比较版本号的能力,网站上还有许多其他答案可以告诉您如何执行此操作:

因此,您只需将版本号保留为 String

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档