swift - iOS - 将字符串转换为 double

标签 swift string firebase double firebase-remote-config

所以我正在从 fire base 配置数据中获取并尝试这样操作它。

这是我的结构:

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”,termOfUrlFireBase:“https://www.name.com/corporate/terms-of-use ")

1.0.2 1.0.2 零 无

有什么想法吗?

最佳答案

它是一个简单的 String,但它实际上不是一个有效的 Double(Double 值没有两位小数)。所以这就是它失败的原因。

我认为您真正需要的是比较版本号的能力,网站上还有许多其他答案可以告诉您如何做到这一点:

所以你可以将你的版本号保存为 String

关于swift - iOS - 将字符串转换为 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53157176/

相关文章:

swift - 仅从 firebase 获取特定数据

swift - 错误域=NSCocoaErrorDomain 代码=257 无法打开 fil ""- Firebase

ios - XLPagerTabStrip:动态添加 subview 到选项卡的 View Controller 不会显示,除非在第一个选项卡下

swift - 初始化(编码器 :) has not been implemented in swift

java - 类似于 spring 的 NamedParameterUtils 的工具用于常规字符串处理?

python - 如何获取关键字后的字符串

javascript - Firestore 更新数组字段中的单个项目

objective-c - 使用 StoreCard.pass 初始化时 PKPass 返回 nil

ios - 如何保存倒数计时器的进度?

C++ plus 运算符不适用于 const char 和其他运算符