Search code examples
swiftstringfirebasedoublefirebase-remote-config

iOS - Converting String to Double


So i am fetching from fire base config data and trying to manipulate it as so.

This is my struct :

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

This is my parser method:

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
}

And this is the implementation in the 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.")")
        }
    })

so the prints are so:

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 nil nil

any ideas?


Solution

  • It is a simple String, but it's not actually a valid Double (Double values do not have two decimal places). So this is why it is failing.

    What I think you actually need is the ability to compare version numbers, there are a number of other answers on the site that can show you how to do this:

    So you can just keep your version numbers as a String