Swiftpack.co - Misoservices/MisoViewOptional as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by Misoservices.
Misoservices/MisoViewOptional 1.0.0
SwiftUI package to allow for conditional compilation of modifiers
⭐️ 5
🕓 2 years ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/Misoservices/MisoViewOptional.git", from: "1.0.0")

MisoViewOptional

SwiftUI package to allow for conditional compilation of modifiers

This is the simplest Swift package ever. It's mostly a design pattern to allow for an optional modifier that returns an AnyView.

Why?

When you compile something conditionally, sometimes, a modifier is not available in a platform. For example, macOS doesn't support navigationBarItems, and you cannot simply do

VStack {
   Spacer()
}
#if !os(macOS)
.navigationBarItems(...)
#endif

Also, the usual pattern of setting someting to nil doesn't actually work.

There are also some other uses, especially when some modifiers don't support a nil default. You can if its usage completely away through the .optional modifier.

Why AnyView?

Not only is this not hurting the performance, but also, due to Swift's lack of preprocessor, the #if is actually defined as a command. And because the #else is undefined, the system cannot determine what should be the return type. There might be a better design pattern, and please don't hesitate to provide a better implementation.

Usage

import MisoViewOptional

VStack {
    Spacer()
}
.frame(width: 123, height: 456)
.optional() { content in
    #if os(macOS)
    return AnyView(content)
    #else
    return AnyView(content.navigationBarTitle("MyTitle")
                          .navigationBarItems(trailing: doneButton))
    #endif
}

Colophon

The official address for this package

The git / package url

This package is created and maintained by Misoservices Inc. and is licensed under the BSL-1.0: Boost Software License - Version 1.0.

GitHub

link
Stars: 5
Last commit: 2 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

Initial release
4 years ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics