-1

I'm comparing appstore version with version in the app build. By mistake I entered 15 instead of 15.0 in the build version in the app code. Following compare fails

appStoreAppVersion = "15.0"

currentVersion = "15"

if appStoreAppVersion.compare(currentVersion, options: .numeric) == .orderedDescending {  
}

Using above compare gives me "15.0" > "15" so it fails. I thought "15.0" will be equal to "15"

How to fix this compare function so that it takes care of the decimal values for checking version? Is there any existing compare function which can take care of the decimal values as well?

Raymond
  • 1,108
  • 13
  • 32
  • didn't fully get what was the problem here, but it seems you're trying to compare versions string. Here is another post, with this (https://stackoverflow.com/a/44361022/5464805) answer that can be helpful to you – Olympiloutre Mar 09 '22 at 10:37
  • Using above compare gives me "15.0" > "15" so it fails. I thought "15.0" will be equal to "15". Yes, I've already seen that example but using a pod just co compare strings is overkill, is there any other elegant solution? – Raymond Mar 09 '22 at 10:38
  • wasn't necessary suggesting to download the whole pod but maybe you'll find some inspiration in those methods. if `compare` returns tells you that "15.0" > "15", then it's the expected behaviour when it comes to version string comparison. Another thing you could try is an extension of string that would automatically add the ".0". example: `currentVersion.asVersionString()` in which you parse your string, and if it doesnt contains a minor version number, it automatically adds ".0". This, "15" becomes "15.0" and the comparison will not fail – Olympiloutre Mar 09 '22 at 10:47
  • Even more elegant could be a `struct Version {}` with 3 ints, major, minor and patch, with a `toString` and a `compare` method (that can indeed rely on string compare). By default both minor and patch are at 0 so everything can be covered. – Olympiloutre Mar 09 '22 at 10:50
  • Yes, you are right. It would be simpler if I just follow versioning guidelines. – Raymond Mar 09 '22 at 10:50

1 Answers1

1

Following what we discussed in comment. You could do something like that :

import UIKit

struct Version: Comparable {
    let major: Int
    let minor: Int
    let patch: Int
    
    init(versionString: String) {
        let categories = versionString.components(separatedBy: ".");
        
        major = Int(categories[0]) ?? 0
        minor = (categories.count > 1) ? Int(categories[1]) ?? 0 : 0
        patch = (categories.count > 2) ? Int(categories[2]) ?? 0 : 0
    }
    
    func asString() -> String {
        return "\(major).\(minor).\(patch)"
    }
    
    static func < (lhs: Version, rhs: Version) -> Bool {
        if (lhs.major < rhs.major) { return true }
        if (lhs.minor < rhs.minor) { return true }
        if (lhs.patch < rhs.patch) { return true }

        return false;
    }
}

let versionA = Version(versionString: "16")
let versionB = Version(versionString: "16.0")

versionA == versionB




This gives you full control over your versions.


Edit: tried a bit more and found ou the string compare didn't work in this case, please use the new comparison.

What's missing in this code if you want to go further:

  • error handling when the user passes a string that is not a version string
  • could do an extension directly in string like asVersion() which returns a Version that you can compare straight away
Olympiloutre
  • 2,268
  • 3
  • 28
  • 38