NavigationLink init(_:destination:) deprecated

NavigationLink's initializer

init<S>(_ title: S, destination: Destination) where S : StringProtocol

is deprecated and only available for iOS 13.0–15.2. The replacement is

init<S>(_ title: S, destination: () -> Destination) where S : StringProtocol

per the documentation: https://developer.apple.com/documentation/swiftui/navigationlink/init(_:destination:)-6hslu.

Replacing my existing code with

NavigationLink("MyTitle") {
    Text("MyView")
}

shows the error

Type '() -> Text' cannot conform to 'View'
1. Only concrete types such as structs, enums and classes can conform to protocols
2. Required by generic struct 'NavigationLink' where 'Destination' = '() -> Text'

What's wrong with my use of the recommended initializer?

Using Xcode Version 12.5.1 (12E507). Here is the full view for reference:

import SwiftUI

struct Test: View {
  var body: some View {
    NavigationView {
      NavigationLink("MyTitle") {
        Text("MyView")
      }
    }
  }
}
Answered by ryguyactor in 698822022

Updating from Xcode 12.5.1 (12E507) to 13.2.1 (13C100) fixed this.

The SwiftUI source in Xcode 12 did not contain the NavigationLink initializers with the destination parameter as a closure type; Xcode 13 did.

I tried your code with Xcode 12.5.1 and it shows the same error. And trying with Xcode 13.1, it compiles without any problems. I'm not sure what is causing this issue, but until the problem is solved, you may need to use Xcode 13.x .

Accepted Answer

Updating from Xcode 12.5.1 (12E507) to 13.2.1 (13C100) fixed this.

The SwiftUI source in Xcode 12 did not contain the NavigationLink initializers with the destination parameter as a closure type; Xcode 13 did.

NavigationLink init(_:destination:) deprecated
 
 
Q