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
🕓 5 weeks ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/Misoservices/MisoViewOptional.git", from: "1.0.0")


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.


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 {
#if !os(macOS)

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.


import MisoViewOptional

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


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.


Stars: 5
Last commit: 5 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

Initial release
1 year ago

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