CaptureContext/cocoa-aliases
A small Swift package that provides Cocoa-prefixed typealiases for AppKit and UIKit types
Table of contents
Motivation
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.
Usage
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.
Installation
Basic
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.
Recommended
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
License
This library is released under the MIT license. See LICENSE for details.
Package Metadata
Repository: CaptureContext/cocoa-aliases
Stars: 1
Forks: 0
Open issues: 0
Default branch: main
Primary language: swift
License: MIT
Topics: aliases, appkit, cocoa, cocoa-touch, multiplatform, swift, typealiases
README: README.md