2

I have a model like so...

struct User: Codable {
    let followersCount: Int
    let followingCount: Int
}

Using NavigationStack I would like to be able to use NavigationLink based on Value

 NavigationLink(value: user.followersCount) {
    Text("Followers")
}

NavigationLink(value: user.followingCount) {
    Text("Following")
}
.navigationDestination(for: Int.self) { _ in
    FollowersView()
}

.navigationDestination(for: Int.self) { _ in
    FollowingView()
}

As both values are an Int. Is there a way to differentiate the two?

1 Answer 1

7

Declare an object:

enum Link {
   case followers(Int)
   case following(Int)
}

then use it like:

NavigationLink(value: Link.followers(user.followersCount)) {
    Text("Followers")
}

NavigationLink(value: Link.following(user.followingCount)) {
    Text("Following")
}

.navigationDestination(for: Link) { link in
    switch link {
    case let .followers(count):
        FollowersView()
    case let .following(count):
        FollowingView()
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.