Skip to content

CaptureContext/cocoa-aliases

Repository files navigation

cocoa-aliases

CI

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
  }
}
#endif

With 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

  1. From the File menu, select Swift Packages › Add Package Dependency…
  2. Enter "https://github.com/capturecontext/cocoa-aliases" into the package repository URL text field
  3. 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.

About

A small Swift package that provides Cocoa-prefixed typealiases for AppKit and UIKit types

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages