3

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?

ironRoei
  • 2,049
  • 24
  • 45
  • 4
    "1.0.2" is not convertiable to a Double, therefore the Double initializer that takes a string just returns `nil`. – Andreas Oetjen Nov 05 '18 at 15:25
  • So what you suggest and why isn't it convertible ? it is a simple string – ironRoei Nov 05 '18 at 15:26
  • 3
    yes, it's simple `String`... but it's *not* a `Double`. `Double` values do not have *two* decimal points. I think what you might be looking for is: [Compare two version strings in Swift](https://stackoverflow.com/questions/27932408/compare-two-version-strings-in-swift) – Scriptable Nov 05 '18 at 15:27
  • You right , any suggestion hot to check between 2 version? to what am I supposed to convert to sting – ironRoei Nov 05 '18 at 15:33
  • 1
    https://stackoverflow.com/questions/1978456/compare-version-numbers-in-objective-c or the link from @Scriptable comment. – Larme Nov 05 '18 at 15:39

1 Answers1

1

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

Scriptable
  • 19,402
  • 5
  • 56
  • 72