1

I am a beginner at SwiftUI.

I created a struct to save some text and the detail information link page. like this:

struct First: Identifiable {
    let title: String 
    let icon: String
    let id = UUID()
    let link: View //link to customised page

the property link will save the View I have created as Page1.swift, Page2.swift, Page2.swift...

How to define the link type in the struct First?

Thanks a lot.

1
  • 2
    I don’t think having View as a type of a property is a good idea. What is it that you want to achieve with this link? Commented Sep 19, 2022 at 6:20

3 Answers 3

1
  1. You can use AnyView(not recommended):
struct First: Identifiable {
    let title: String
    let icon: String
    let id = UUID()
    let link: AnyView
  1. Using generics:
struct First<Content: View>: Identifiable {
    let title: String
    let icon: String
    let id = UUID()
    let link: Content
  1. You could instead create an enum:
enum First: Int, Identifiable {
    //your cases
    var id: Int {
        return rawValue
    }
    var title: String {
        switch self {
            //cases
        }
    }
    var icon: String {
        switch self {
            //cases
        }
    }
    var link: some View {
        switch self {
            //cases
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I use something similar in my code. I use AnyView (not recommended by Apple)

struct First: Identifiable {
    let title: String
    let icon: String
    let id = UUID()
    let link: AnyView
}

and when I call the view, I do

AnyView(MyView()) 

OR (thanks Timmy)

struct First<Content: View>: Identifiable {
    let title: String
    let icon: String
    let id = UUID()
    let link: Content
}

and call the view with

First(title: "xxx", icon: "xxx", link: View1())

The second method does not work if it is included in a static property.

Error : Static stored properties not supported in generic types

3 Comments

It's not recommended to use AnyView: developer.apple.com/videos/play/wwdc2021/10022
how you call the view with generic ?
Like this: First(title: "", icon: "", link: View1())
0

Thanks @Timmy, Thanks @dibs,

I use this define and the program run with code First(title: "xxx", icon: "xxx", link: AnyView(View1())), First(title: "xxx", icon: "xxx", link: AnyView(View2())), as below:

struct First: Identifiable {
    let title: String
    let icon: String
    let id = UUID()
    let link: AnyView
}

but the second way, there is some wrong when I call views by below way: First(title: "xxx", icon: "xxx", link: View1()), First(title: "xxx", icon: "xxx", link: View2()),

I try to use the way to call view by link: AnyView(View1()), link: AnyView(View2()), it success.

struct First<Content: View>: Identifiable {
    let title: String
    let icon: String
    let id = UUID()
    let link: Content

Thanks the help from @Timmy, @dibs

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.