Observe objects in SwiftUI Views which may be nil

Overview

ObservedOptionalObject

Swift

Rationale

SwiftUIs @ObservedObject requires that the observed object actually exists. In some cases it's convenient to observe an object which might be nil. In this case, @ObservedOptionalObject can be used.

struct SomeView: View {
    // Instead of
    @ObservedObject var anObject: Model? // Won't work
    
    // use
    @ObservedOptionalObject var anObject: Model?
    
    var body: some View {
        HStack {
            Text("Name")
            if let name = anObject?.name {
                Text(name)
            }
        }
    }
}

Please note, that @ObservedOptionalObject is only useful if your contains content that should be displayed, even when the object is nil. Otherwise the view should be contained within an if let statement: if let obj = obj { SomeView(anObject: obj) }

Installation

This package is available via SwiftPM

dependencies: [
    .package(url: "https://github.com/fourplusone/observed-optional-object.git", 
        .upToNextMinor(from: "0.1.0")
    )
]

License

This project is licensed under the MIT License.

You might also like...
FancyGradient is a UIView subclass which let's you animate gradients in your iOS app. It is purely written in Swift.
FancyGradient is a UIView subclass which let's you animate gradients in your iOS app. It is purely written in Swift.

FancyGradient is a UIView subclass which let's you animate gradients in your iOS app. It is purely written in Swift. Quickstart Static gradient let fa

Unit-Converter-SwiftUI - A simple Unit Converter iOS app built in the process of learning SwiftUI
Unit-Converter-SwiftUI - A simple Unit Converter iOS app built in the process of learning SwiftUI

SwiftUI-Unit-Converter A simple Unit Converter iOS app built in the process of l

Recreating a fully functional version of iOS 4 in SwiftUI.
Recreating a fully functional version of iOS 4 in SwiftUI.

Updates: Version 1.0 is currently available 🎉 While I work on fixing an issue in Xcode 12.5+ with LazyVGrid, I recommend you build using Xcode 12.4 a

I needed to detect idle mode on a SwiftUI app for a kiosk, so I did it.

Inactivity I needed to detect idle mode on a SwiftUI app for a kiosk, so I did it. Usage Important: You must set the Principal class of your app to In

SnippetsLibrary - Code snippets library for SwiftUI Devs.
SnippetsLibrary - Code snippets library for SwiftUI Devs.

SnippetsLibrary is a helpful tool for SwiftUI developers to help with their daily coding life. SnippetsLibrary contains all the needed code snippets for you to view, edit, or add more and more. This will make your daily work easier and faster.

🟣 Verge is a very tunable state-management engine on iOS App (UIKit / SwiftUI) and built-in ORM.
🟣 Verge is a very tunable state-management engine on iOS App (UIKit / SwiftUI) and built-in ORM.

Verge is giving the power of state-management in muukii/Brightroom v2 development! Verge.swift 📍 An effective state management architecture for iOS -

Pigeon is a SwiftUI and UIKit library that relies on Combine to deal with asynchronous data.

Pigeon 🐦 Introduction Pigeon is a SwiftUI and UIKit library that relies on Combine to deal with asynchronous data. It is heavily inspired by React Qu

Safe and fast access to SwiftUI PreviewDevice

SafePreviewDevice Motivation At WWDC 2019, Apple announced SwiftUI a new library for building UI in a simple and fast way. Xcode’s SwiftUI preview let

Customize and resize sheets in SwiftUI with SheeKit. Utilise the power of `UISheetPresentationController` and other UIKit features.
Customize and resize sheets in SwiftUI with SheeKit. Utilise the power of `UISheetPresentationController` and other UIKit features.

SheeKit Customize and resize sheets in SwiftUI with SheeKit. Utilise the power of UISheetPresentationController and other UIKit features. Overview She

Comments
  • iOS 13

    iOS 13

    There does not appear to be any reason for why the class should be @available only on iOS 14+.

    Please consider relaxing the @available rule to what is actually required by the implementation & in-line with 74aa40a398688b90ee7724797861185e5a696aca :–)

    wontfix 
    opened by lhunath 4
Releases(v0.1.1)
Owner
Matthias Bartelmeß
Matthias Bartelmeß
Highlighter will magically find UI objects such as UILabel, UITextView, UITexTfield, UIButton

Highlighter Updates See CHANGELOG for details Intoduction ?? Highlight whatever you want! Highlighter will magically find UI objects such as UILabel,

Kyle Yi 932 Dec 12, 2022
Type-Safe Associated Objects in Swift

Type-Safe Associated Objects in Swift TSAO is an implementation of type-safe associated objects in Swift. Objective-C associated objects are useful, b

Lily Ballard 135 Dec 21, 2022
Getting square objects down round holes

Enough with the bazillion lines of array parsing logic. YOLO. @import YOLOKit; campaigns.reject(^(PPCampaign *campaign){ return campaign.locked;

Max Howell 662 Nov 20, 2022
A Swift collection of unique, ordered objects

Introduction OrderedSet is essentially the Swift equivalent of Foundation's NSOrderedSet/NSMutableOrderedSet. It was created so Swift would have a uni

Weebly 248 Sep 14, 2022
A Swift property wrapper which stores the previous value

swift-with-previous A Swift property wrapper which stores the previous value. The previous value can be get by the projected value $propertyName. impo

IKEDA Sho 3 Feb 22, 2022
Extensions which helps to convert objc-style target/action to swifty closures

ActionClosurable Usage ActionClosurable extends UIControl, UIButton, UIRefreshControl, UIGestureRecognizer and UIBarButtonItem. It helps writing swift

takasek 121 Aug 11, 2022
CombineDriver - Drivers are Publishers which don't complete

CombineDriver Drivers are Publishers which don't complete. CombineDriver is a sm

GOOD HATS LLC 0 May 21, 2022
A NEWS app which can be used to read,share and bookmark articles of various categories

Scoop A NEWS App for iOS 14 built using Swift which allow the users to read,bookmark and share news articles. Built using MVC architecture Requirement

Sai Balaji 3 Oct 12, 2022
A Swift wrapper for URL bookmarks which allow a file to be located regardless of whether it is moved or renamed.

Bookmark A Swift wrapper for URL bookmarks which allow a file to be located regardless of whether it is moved or renamed. This class wraps Swift's URL

Darren Ford 8 Nov 3, 2022
Swift Package Manager plugin which runs ActionBuilder to create a Github Actions workflow for a swift package.

ActionBuilderPlugin A Swift Package Manager command which builds a Github Actions workflow for the current package. By default the workflow file will

Elegant Chaos 4 Jul 20, 2022