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

时间:2018-11-05 15:18:22

标签: swift string firebase double firebase-remote-config

因此,我正在从火力基础配置数据中获取数据,并试图以此进行操作。

这是我的结构:

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(更新时间:“ 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 零 零

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这是一个简单的String,但实际上不是有效的DoubleDouble值没有两位小数)。所以这就是为什么它失败了。

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

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