How to get @Environment or @EnvironmentObject inside ObservableObject
Saw this interesting question on Stackoverflow and thought I would answer it. The trick is to use `DynamicProperty as an intermediary as follows:
Saw this interesting question on Stackoverflow and thought I would answer it. The trick is to use `DynamicProperty as an intermediary as follows:
Update June 2022: windowing APIs have now been added so the code below might not be the best way to implement it. Here is how to open a new window or re-activate an existing one in SwiftUI on macOS. In your ContentView create a button and open a URL for Read more…
Update 30/6/2022: I realised we shouldn’t be initing NSPredicate objects inside body so need a new approach. I was reading Paul Hudson’s article Dynamically filtering @FetchRequest with SwiftUI and thought I would share an improvement for declaring the singers @FetchRequest property that supports a dynamic predicate for filtering: FilteredList.swift ContentView.swift
If we want to use @AppStorage but with a dynamic key passed to the View we can do this: DynamicKeyApp.swift ContentView.swift
Here is one way to implement @AppStorage using a custom UserDefaults suite by way of the defaultAppStorage View modifier and a class extension. GroupDefaultsTestApp.swift ContentView.swift
MKMapSnapshotter has the following constructor (within NS_ASSUME_NONNULL_BEGIN): – (instancetype)initWithOptions:(MKMapSnapshotOptions *)options NS_DESIGNATED_INITIALIZER; Which means an options param must be supplied. However the documentation contradicts this, stating “If you specify nil for this property, the snapshotter uses a set of default options that capture an image of the current user’s country”, thus Read more…
Always wondered why NSFetchedResultsChangeUpdate is so buggy? The reason is Apple don’t actually use it in their own apps. Instead, what they do is have a UITableViewCell subclass with a custom property setter for the object where they add KVO for only the properties they are interested in updates to, Read more…
There is a new, yet not very well-known way to override NSManagedObject properties without needing to manually call KVO methods willChangeValueForKey etc. This is achieved using dynamic accessors prefixed with managedObjectOriginal as follows: Department.h @interface Department : NSManagedObject @property (nullable, nonatomic, copy) NSString *name; @end Department.m @interface Department (DynamicAccessors) – (NSString Read more…
When creating a UIViewController subclass, e.g. in a framework, which you require to support being created from both storyboards and code, it is useful to implement initialisation which works for both. That can be achieved with a common init routine as follows: – (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super Read more…
By default, screenshots save to the Desktop which isn’t ideal. With this terminal command you can change the folder, e.g. to the Pictures folder: defaults write com.apple.screencapture location ~/Pictures To remove the setting and go back to the default do: defaults delete com.apple.screencapture location To query what the current setting Read more…