A small Swift package that provides Cocoa-prefixed typealiases for AppKit and UIKit types.
Working with cross-platform UI code often requires conditional compilation to distinguish between AppKit and UIKit types.
This package defines a set of Cocoa-prefixed typealiases that map to the appropriate platform-specific types, allowing shared code to be written without #if os(...)
Note
UIKit and AppKit are not 1:1 equivalents, and some APIs are conditionally available across platforms. While the most common aliases are provided, some may be missing. Feel free to request additional aliases via Issues or contribute them with a pull request.
Without this package, cross-platform extensions typically require conditional compilation:
#if os(macOS)
extension NSView {
func rounded() {
layer.cornerRadius = min(bounds.width, bounds.height) / 2
}
}
#else
extension UIView {
func rounded() {
layer.cornerRadius = min(bounds.width, bounds.height) / 2
}
}
#endifWith cocoa-aliases, the same code can be written once:
import CocoaAliases
extension CocoaView {
func rounded() {
layer.cornerRadius = min(bounds.width, bounds.height) / 2
}
}The Cocoa* aliases resolve to NS* types on macOS and UI* types on iOS, tvOS, and watchOS.
Tip
CocoaAliases also export Cocoa-prefixed marker protocols.
You can add cocoa-aliases to an Xcode project by adding it as a package dependency
- From the File menu, select Swift Packages › Add Package Dependency…
- Enter
"https://github.com/capturecontext/cocoa-aliases"into the package repository URL text field - Choose products you need to link to your project.
If you use SwiftPM for your project structure, add cocoa-aliases dependency to your package file.
.package(
url: "https://github.com/capturecontext/cocoa-aliases.git",
.upToNextMinor("3.3.0")
)Do not forget about target dependencies:
.product(
name: "CocoaAliases",
package: "cocoa-aliases"
)Note
The package is compatible with non-Apple platforms, however it uses conditional compilation, so APIs are only available on Apple platforms
This library is released under the MIT license. See LICENSE for details.