How to determine if a passed argument is a string literal?

I'm not 100% sure about how SwiftUI is actually doing it since I'm not seeing any explicit attribute on those initializers, but you can achieve it using the currently private @_disfavoredOverload attribute:

struct MyView: View {
  let title: Text

  @_disfavoredOverload
  init<S: StringProtocol>(_ content: S) {
    title = Text("not localized")
  }

  init(_ key: LocalizedStringKey) {
    title = Text("localized")
  }

  var body: some View {
    title
  }
}

Within your example, it will output:

not localized
localized
localized
localized
2 Likes