I have an app created from Xcode 9 / New Project / Cross Platform Game App, targets: macOS and iOS.
I wrote the following simple function to load text from a Data Asset.
func dataAssetAsString(_ name: String) -> String? {
if let asset = NSDataAsset(name: NSDataAsset.Name(name)) {
return String(data: asset.data, encoding: .utf8)
} return nil
}I'm puzzled by the following things:
1. This function only works if I import either UIKit, or AppKit.
2. But somehow importing MetalKit instead of UIKIt or AppKit makes it work. This really puzzles me. This should have nothing to do with MetalKit.
Most answers I found for code sharing between iOS and macOS suggest some kind of conditional import at the beginning of the file / shimming.
I went through the slides of WWDC 2014 Session 233: Sharing code between iOS and OS X, and it explicitly says we should not do shimming in Swift.
On the other hand, I don't understand what we should do instead of shimming. The mentioned very complicated process of creating and compiling shared frameworks for a 5 line function cannot be the right direction.
1. In 2018 (Swift 4, Xcode 9, iOS 11, macOS 10.13), what would be the recommended way to add such a trivial cross platform code to an app?
2. What is the magic of MetalKit which makes it work without shimming? Does it shim internally?