File tree Expand file tree Collapse file tree 3 files changed +13
-6
lines changed
CoreFoundation/Base.subproj Expand file tree Collapse file tree 3 files changed +13
-6
lines changed Original file line number Diff line number Diff line change @@ -315,6 +315,7 @@ static CFStringRef copySystemVersionPath(CFStringRef suffix) {
315
315
316
316
317
317
CFDictionaryRef _CFCopySystemVersionDictionary (void ) {
318
+ // TODO: Populate this dictionary differently on non-Darwin platforms
318
319
CFStringRef path = copySystemVersionPath (CFSTR ("/System/Library/CoreServices/SystemVersion.plist" ));
319
320
CFPropertyListRef plist = _CFCopyVersionDictionary (path );
320
321
CFRelease (path );
Original file line number Diff line number Diff line change @@ -89,21 +89,26 @@ public class NSProcessInfo : NSObject {
89
89
90
90
public var operatingSystemVersionString : String {
91
91
get {
92
- return CFCopySystemVersionString ( ) . _swiftObject
92
+ return CFCopySystemVersionString ( ) ? . _swiftObject ?? " Unknown "
93
93
}
94
94
}
95
95
96
96
public var operatingSystemVersion : NSOperatingSystemVersion {
97
97
get {
98
- let productVersionKey = unsafeBitCast ( _kCFSystemVersionProductVersionKey, UnsafePointer< Void> . self )
99
- let productVersion = unsafeBitCast ( CFDictionaryGetValue ( _CFCopySystemVersionDictionary ( ) , productVersionKey) , NSString!. self)
100
98
// The following fallback values match Darwin Foundation
101
99
let fallbackMajor = - 1
102
100
let fallbackMinor = 0
103
101
let fallbackPatch = 0
104
- if productVersion == nil {
102
+
103
+ guard let systemVersionDictionary = _CFCopySystemVersionDictionary ( ) else {
104
+ return NSOperatingSystemVersion ( majorVersion: fallbackMajor, minorVersion: fallbackMinor, patchVersion: fallbackPatch)
105
+ }
106
+
107
+ let productVersionKey = unsafeBitCast ( _kCFSystemVersionProductVersionKey, UnsafePointer< Void> . self )
108
+ guard let productVersion = unsafeBitCast ( CFDictionaryGetValue ( systemVersionDictionary, productVersionKey) , NSString!. self) else {
105
109
return NSOperatingSystemVersion ( majorVersion: fallbackMajor, minorVersion: fallbackMinor, patchVersion: fallbackPatch)
106
110
}
111
+
107
112
let versionComponents = productVersion. _swiftObject. characters. split ( " . " ) . flatMap ( String . init) . flatMap ( { Int ( $0) } )
108
113
let majorVersion = versionComponents. dropFirst ( 0 ) . first ?? fallbackMajor
109
114
let minorVersion = versionComponents. dropFirst ( 1 ) . first ?? fallbackMinor
Original file line number Diff line number Diff line change @@ -29,9 +29,10 @@ class TestNSProcessInfo : XCTestCase {
29
29
func test_operatingSystemVersion( ) {
30
30
let processInfo = NSProcessInfo . processInfo ( )
31
31
let versionString = processInfo. operatingSystemVersionString
32
+ XCTAssertFalse ( versionString. isEmpty)
33
+
32
34
let version = processInfo. operatingSystemVersion
33
- let reconstructedVersionString = " Version \( version. majorVersion) . \( version. minorVersion) . \( version. patchVersion) "
34
- XCTAssert ( versionString. hasPrefix ( reconstructedVersionString) )
35
+ XCTAssertNotNil ( version. majorVersion != 0 )
35
36
}
36
37
37
38
func test_processName( ) {
You can’t perform that action at this time.
0 commit comments