<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 15 Feb 2017, at 20:40, Greg Parker <<a href="mailto:gparker@apple.com" class="">gparker@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><blockquote type="cite" style="font-family: SFUIText-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">On Feb 15, 2017, at 11:11 AM, Ole Begemann via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:<br class=""><br class="">In macOS 10.12 and iOS 10.0, class properties were introduced to Objective-C [1]. I noticed that the Objective-C runtime treats class properties differently based on the deployment target. Example:<br class=""><br class=""> // MyClass is an Objective-C class that has a class property<br class=""> let metaclass = object_getClass(MyClass.self)<br class=""> var count: UInt32 = 0<br class=""> let properties = class_copyPropertyList(metaclass, &count)<br class=""> // Work with properties<br class=""> // Deallocate properties<br class=""><br class="">When the deployment target is macOS 10.12, passing a metaclass to copyPropertyList() returns the class properties, but it returns an empty list on a lower deployment target.<br class=""></blockquote><br style="font-family: SFUIText-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: SFUIText-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">That's right. The Objective-C runtime was written with the expectation that class properties would be added someday, but that code was buggy. On macOS 10.10 and iOS 8.x and older if libobjc sees a class property then it may crash. The fix was for clang and swiftc to leave class properties out of the ObjC metadata when compiling for those deployment targets.<span class="Apple-converted-space"> </span></span><br style="font-family: SFUIText-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: SFUIText-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: SFUIText-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">macOS 10.11 and iOS 9.x deployment targets are both safe. (Class properties still did not exist then. Instead the bug that nobody had seen yet was unknowingly fixed when the code was rewritten for an unrelated optimization.)</span><br style="font-family: SFUIText-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote><div><br class=""></div>Thank you, Greg. I only tested with 10.12 and 10.10, so I hadn't noticed that it would already work with 10.11.</div><div><br class=""><blockquote type="cite" class=""><div class=""><span style="font-family: SFUIText-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Note that class property visibility depends on the deployment target used to compile that class's implementation. It is possible to see class properties from some classes but not others if the classes come from different executables with different deployment targets.</span><br style="font-family: SFUIText-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: SFUIText-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" style="font-family: SFUIText-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">I'd like to perform a check (either at runtime or compile time) which of these behaviors I'll get.<br class=""><br class="">I didn't find a way to perform a compile-time check in Swift against the deployment target; if #available() checks against the SDK the code is linked against. Likewise, the usual runtime check using ProcessInfo().isOperatingSystemAtLeast() doesn't check against the deployment target.<br class=""><br class="">The best I came up with is this:<br class=""><br class="">/**<br class="">Returns `true` if the current deployment target is at least the specified version that corresponds to the current platform.<br class=""><br class="">The arguments must be passed in the form of the `__MAC_XX_X`, `__IPHONE_XX_X`, etc. constants defined in Availability.h.<br class="">*/<br class="">public func isDeploymentTargetAtLeast(<br class=""> macOS macOSVersion: Int32, iOS iOSVersion: Int32,<br class=""> tvOS tvOSVersion: Int32, watchOS watchOSVersion: Int32) -> Bool {<br class=""><br class=""> #if os(macOS)<br class=""> return __MAC_OS_X_VERSION_MIN_REQUIRED >= macOSVersion<br class=""> #elseif os(iOS)<br class=""> return __IPHONE_OS_VERSION_MIN_REQUIRED >= iOSVersion<br class=""> #elseif os(tvOS)<br class=""> return __TV_OS_VERSION_MIN_REQUIRED >= tvOSVersion<br class=""> #elseif os(watchOS)<br class=""> return __WATCH_OS_VERSION_MIN_REQUIRED >= watchOSVersion<br class=""> #else<br class=""> return false<br class=""> #endif<br class="">}<br class=""><br class="">For example, to test if the current deployment target is at least macOS 10.12, iOS 10.0, tvOS 10.0, or watchOS 3.0:<br class=""><br class="">guard isDeploymentTargetAtLeast(<br class=""> macOS: __MAC_10_12, iOS: __IPHONE_10_0,<br class=""> tvOS: __TVOS_10_0, watchOS: __WATCHOS_3_0) else {<br class=""><br class=""> // Deployment target is lower<br class=""> ...<br class="">}<br class=""><br class="">Is this correct? Is there a better way to do this?<br class=""></blockquote><br style="font-family: SFUIText-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: SFUIText-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">That code ought to work, assuming that the implementation of isDeploymentTargetAtLeast() and the implementations of the interrogated classes are in the same executable, or are all in executables compiled with the same deployment target.</span><br style="font-family: SFUIText-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote></div><br class=""></body></html>