Home > Software engineering >  Is there anybody who could help me to convert Objective-C UIDevice SystemVersion into Swift?
Is there anybody who could help me to convert Objective-C UIDevice SystemVersion into Swift?

Time:11-22

I don't know how to convert the following Objective-C code into Swift as I have not worked with Objective-C before. How should I do it? If somebody has encountered something similar, any help of how the Objective-C code below would look like in Swift would be much appreciated.

// header (.h) file:

#import <UIKit / UIKit.h>

@interface UIDevice(SystemVersion)

  - BOOL systemVersionLessThan: (NSString *) target

@end
// implementation (.m) file

#import "UIDevice SystemVersion.h"

@implementation UIDevice(SystemVersion)

  - BOOL systemVersionLessThan: (NSString *) target {
    [[self systemVersion] compare: target options: NSNumericSearch] == NSOrderedAscending
  }

I can't seem to get my head around of the right way to do this in Swift. This more likely is way off it really should be.

import UIKit

extension UIDevice {
  func SystemVersion() {
    if systemVersionLessThan ->  {}
  }
}

CodePudding user response:

The literal translation is

extension UIDevice {
    func systemVersionLessThan(_ target: String) -> Bool {
        systemVersion.compare(target, options: .numeric) == .orderedAscending
    }
}

You can also use

extension UIDevice {
    func systemVersionLessThan(_ target: String) -> Bool {
        systemVersion.localizedStandardCompare(target) == .orderedAscending
    }
}
  • Related