update-xcode-version-action Versions v0.1 to v0.2.0
Pull request Do not reset backgroundView when delegate is nil on composed-swift/Composed
Partial fix for #31
Pull request Work around memory leak when checking for `nil` on composed-swift/Composed
Added to work around https://bugs.swift.org/browse/SR-14103
Pull request Cache `SingleElementSections.numberOfElements` on composed-swift/Composed
Overall this is pretty minor but when dealing with a lot of SingleElementSections
that are infrequently updated is does start to slow down.
I thought a faster approach would be to use generics on replace(element:)
to only check for nil
when the value is Optional
, but generics don't allow for this kind of overload.
Also added some basic tests. The performance tests show a ~9% performance increase.
Pull request Batch collection view updates on composed-swift/Composed
Replacing https://github.com/composed-swift/ComposedUI/pull/15
Pull request Pull in OpenNet changes on composed-swift/Composed
This PR is being used to diff between the OpenNet fork.
Hopefully other PRs will be merged before this (e.g. https://github.com/composed-swift/Composed/pull/21), which should lead to this PR having no changes and purely to ensure some of the history is not lost.
Pull request Update `sectionOffset(for:)` to return optional on composed-swift/Composed
We've discussed returning -1
not being great previously, but now we have the 2.0
branch we can make this change :)
Pull request Update SwiftPM package syntax in documentation on apple/swift-protobuf
- Tags:
- open-source
Updated to match syntax in README.md.
Maybe these were intentionally left because the package has a minimum tools version of 4.2?
Mapping Optional Binding to Bool
When displaying an alert in SwiftUI, if the value used to calculate whether the alert is presented is both Optional
and does not conform to Identifiable
1 it is often recommended to use a separate flag, similar to:
struct ContentView: View { @State private var alertText: String? @State private var isPresentingAlert = false var body: some View { Button("Show Alert") { self.alertText = "Alert Text" self.isPresentingAlert = true } .alert(isPresented: $isPresentingAlert) { Alert(title: Text(alertText!)) } } }
There are 2 main downsides to this:
alertText
is not set back tonil
, which may cause bugs and will increase memory usage (even if only a little in this case)- The
isPresentingAlert
flag needs to be managed
To work around these issues I create a small extension to Binding
the allows this same code to be updated to:
struct ContentView: View { @State private var alertText: String? var body: some View { Button("Show Alert") { self.alertText = "Alert Text" } .alert(isPresented: $alertText.mappedToBool()) { Alert(title: Text(alertText!)) } } }
The extension is fairly small and simple:
import os.log import SwiftUI extension Binding where Value == Bool { /// Creates a binding by mapping an optional value to a `Bool` that is /// `true` when the value is non-`nil` and `false` when the value is `nil`. /// /// When the value of the produced binding is set to `false` the value /// of `bindingToOptional`'s `wrappedValue` is set to `nil`. /// /// Setting the value of the produce binding to `true` does nothing and /// will log an error. /// /// - parameter bindingToOptional: A `Binding` to an optional value, used to calculate the `wrappedValue`. public init<Wrapped>(mappedTo bindingToOptional: Binding<Wrapped?>) { self.init( get: { bindingToOptional.wrappedValue != nil }, set: { newValue in if !newValue { bindingToOptional.wrappedValue = nil } else { os_log( .error, "Optional binding mapped to optional has been set to `true`, which will have no effect. Current value: %@", String(describing: bindingToOptional.wrappedValue) ) } } ) } } extension Binding { /// Returns a binding by mapping this binding's value to a `Bool` that is /// `true` when the value is non-`nil` and `false` when the value is `nil`. /// /// When the value of the produced binding is set to `false` this binding's value /// is set to `nil`. public func mappedToBool<Wrapped>() -> Binding<Bool> where Value == Wrapped? { return Binding<Bool>(mappedTo: self) } }
The extension isn't tied directly to showing an alert or a sheet and can be used in any context, but this is one of the better examples of its usage.
This extension is available on GitHub under the MIT license.
1 If it does conform to Identifiable
use alert(item:content:)