Hi,
I'm trying to add a badge to a Tab
when a certain condition is met. Prior to iOS 18 I could do .badge(showBadge ? "Show" : nil)
. However, in iOS 18 I'm getting the following error message 'nil' cannot be used in context expecting type 'LocalizedStringKey'
.
Right clicking the .badge
-> Jump to Definition
shows the following
/// Generates a badge for the tab from a localized string key.
///
/// Use a badge to convey optional, supplementary information about a
/// view. Keep the contents of the badge as short as possible. The string
/// provided will appear as an indicator on the given tab.
///
/// This modifier creates a ``Text`` view on your behalf, and treats the
/// localized key similar to ``Text/init(_:tableName:bundle:comment:)``. For
/// more information about localizing strings, see ``Text``. The
/// following example shows a tab that has a "New Alerts" badge
/// when there are new alerts.
///
/// var body: some View {
/// TabView {
/// Tab("Home", systemImage: "house") {
/// HomeView()
/// }
/// Tab("Alerts", systemImage: "bell") {
/// AlertsView()
/// }
/// .badge(alertsManager.hasAlerts ? "New Alerts" : nil)
/// }
/// }
///
/// - Parameter key: A string key to display as a badge.
nonisolated public func badge(_ key: LocalizedStringKey) -> some TabContent<Self.TabValue>
Here it looks like that the signature of .badge
has changed to a non-nil LocalizedStringKey
from pre-iOS 18 LocalizedStringKey?
.
Any ideas how to solve this?
I would recommend using the Text
variant of TabContent/badge
, which accepts a nullable badge (https://developer.apple.com/documentation/swiftui/tabcontent/badge(_:)-66pvf)
Thanks for letting us know about the documentation!