How to write common code for iOS and macOS ?
iOS and MacOS share some functionality in some frameworks.
The most used way we can share code between iOS and macOS is by using typealias as our main type naming from iOS to macOS name classes.
For example we had to use UIImage and here is the result that will make your code compile on macOS and iOS :
typealias UIImage = NSImage
// Step 2: You might want to add these APIs that UIImage has but NSImage doesn’t.
extension NSImage {
var cgImage: CGImage? {
var proposedRect = CGRect(origin: .zero, size: size)
return cgImage(forProposedRect: &proposedRect,
context: nil,
hints: nil)
}
convenience init?(named name: String) {
self.init(named: Name(name))
}
}