我正在用Swift检查系统信息。我发现,这可以通过代码来实现:
var sysData:CMutablePointer<utsname> = nil
let retVal:CInt = uname(sysData)
这段代码有两个问题:
sysData的初始值应该是什么?这个例子在retVal中给出-1可能是因为sysData为nil。 如何从sysData读取信息?
我正在用Swift检查系统信息。我发现,这可以通过代码来实现:
var sysData:CMutablePointer<utsname> = nil
let retVal:CInt = uname(sysData)
这段代码有两个问题:
sysData的初始值应该是什么?这个例子在retVal中给出-1可能是因为sysData为nil。 如何从sysData读取信息?
当前回答
斯威夫特5
func run() {
let version = OperatingSystemVersion(majorVersion: 13, minorVersion: 0, patchVersion: 0)
if ProcessInfo.processInfo.isOperatingSystemAtLeast(version) {
runNewCode()
} else {
runLegacyCode()
}
}
func runNewCode() {
guard #available(iOS 13.0, *) else {
fatalError()
}
// do new stuff
}
func runLegacyCode() {
// do old stuff
}
其他回答
我做了帮助函数,从下面的链接转移到swift:
我们如何通过程序检测设备运行的iOS版本?
func SYSTEM_VERSION_EQUAL_TO(version: String) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version,
options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedSame
}
func SYSTEM_VERSION_GREATER_THAN(version: String) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version,
options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedDescending
}
func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version,
options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedAscending
}
func SYSTEM_VERSION_LESS_THAN(version: String) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version,
options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedAscending
}
func SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version,
options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedDescending
}
它可以这样使用:
SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO("7.0")
斯威夫特4.2
func SYSTEM_VERSION_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) == .orderedSame
}
func SYSTEM_VERSION_GREATER_THAN(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) == .orderedDescending
}
func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) != .orderedAscending
}
func SYSTEM_VERSION_LESS_THAN(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) == .orderedAscending
}
func SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) != .orderedDescending
}
在Swift 2及更高版本中,检查系统版本(以及许多其他版本)最简单和最简单的方法是:
if #available(iOS 9.0, *) { // check for iOS 9.0 and later
}
此外,使用#available你可以检查这些版本:
iOS
iOSApplicationExtension
macOS
macOSApplicationExtension
watchOS
watchOSApplicationExtension
tvOS
tvOSApplicationExtension
swift
根据Matt Thompson的回答,这里有一个带有各自单元测试的方法,可以在iOS 7及以上版本(包括iOS 9,不再让你检查NSFoundationNumber)上与Swift和Objective-c一起工作:
+ (BOOL) isAtLeastOSVersion:(NSString *)osVersion
{
switch ([[UIDevice currentDevice].systemVersion compare:osVersion options:NSNumericSearch]) {
case NSOrderedSame:
case NSOrderedDescending:
return YES;
default:
return NO;
}
}
.
@interface ANFakeCurrDevice : NSObject
@property (nonatomic, strong) NSString *systemVersion;
@end
@implementation ANFakeCurrDevice
@end
@implementation MyHelperClassUnitTests
- (void)setUp {
[super setUp];
}
- (void)tearDown {
[super tearDown];
}
- (void)test_isAtLeastOSVersion
{
id deviceMock = [OCMockObject niceMockForClass:[UIDevice class]];
ANFakeCurrDevice *fakeCurrDevice = [ANFakeCurrDevice new];
fakeCurrDevice.systemVersion = @"99.9.9";
[[[deviceMock stub] andReturn:fakeCurrDevice] currentDevice];
XCTAssertTrue([[UIDevice currentDevice].systemVersion isEqualToString:@"99.9.9"]);
fakeCurrDevice.systemVersion = @"1.0.1";
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"1"]);
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"1.0"]);
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"1.0.1"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"1.0.2"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"1.1.0"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"2"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"2.0"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"2.0.0"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"2.0.1"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"2.1.0"]);
fakeCurrDevice.systemVersion = @"8.4.0";
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"7.0.1"]);
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"8"]);
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"8.4"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"8.4.1"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"8.4.2"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"9.0"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"9.0.1"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"9.0.2"]);
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"8.4"] && ![ANConstants isAtLeastOSVersion:@"9.0"]);
fakeCurrDevice.systemVersion = @"8.4.1";
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"8.4"] && ![ANConstants isAtLeastOSVersion:@"9.0"]);
}
@end
这里编写的大多数示例代码都将获得额外零版本的意外结果。例如,
func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) != ComparisonResult.orderedAscending
}
在iOS“10.3”中,该方法不会在传递版本“10.3.0”时返回true。这样的结果是没有意义的,必须视为同一版本。为了得到准确的比较结果,必须考虑比较版本字符串中所有的数字分量。另外,以大写字母提供全局方法并不是一个好方法。因为我们在SDK中使用的版本类型是String,所以在String中扩展比较功能是有意义的。
要比较系统版本,以下所有示例都可以工作。
XCTAssertTrue(UIDevice.current.systemVersion.isVersion(lessThan: "99.0.0"))
XCTAssertTrue(UIDevice.current.systemVersion.isVersion(equalTo: UIDevice.current.systemVersion))
XCTAssertTrue(UIDevice.current.systemVersion.isVersion(greaterThan: "3.5.99"))
XCTAssertTrue(UIDevice.current.systemVersion.isVersion(lessThanOrEqualTo: "10.3.0.0.0.0.0.0"))
XCTAssertTrue(UIDevice.current.systemVersion.isVersion(greaterThanOrEqualTo: "10.3"))
你可以在我的存储库中查看 https://github.com/DragonCherry/VersionCompare
如果你正在使用Swift 2,并且你想要检查操作系统版本以使用某个API,你可以使用新的可用性特性:
if #available(iOS 8, *) {
//iOS 8+ code here.
}
else {
//Code for iOS 7 and older versions.
//An important note: if you use #availability, Xcode will also
//check that you don't use anything that was introduced in iOS 8+
//inside this `else` block. So, if you try to use UIAlertController
//here, for instance, it won't compile. And it's great.
}
我写这个答案是因为它是谷歌中swift 2 check系统版本查询的第一个问题。