Help with documentation : example: DisclosureGroup

when I read the disclosureGroup documentation , I see

Code Block
struct DisclosureGroup<Label, Content> where Label : View, Content : View

in the first part it says label, I dont understand why.
I can put a string and it work,
But
if I put a label I get an error saying it requires that 'Label<Text, Image>' conform to 'StringProtocol'

it get worse (for me :-)
the second part say "where Label : View, Content : View"
for me it says that label conform to view... ?
I tried to put a view, it did not work either , with the same error

if I use the "Jump to definition" of disclosure, I can see this
  public init(@ViewBuilder content: @escaping () -> Content, @ViewBuilder label: () -> Label)

then I am totally lost :-)

in label I can see
Code Block
struct Label<Title, Icon> where Title : View, Icon : View

:-) Title ... a view ?

is there a documentation to help me read documentation :-)

Replies

in the first part it says label, I dont understand why.

In Swift, identifiers are case-sensitive, you should better care about that.

I can put a string and it work, 

I do not understand what you have tried, but you cannot put String as Label, as String does not conform to View.
Please show by code what you have tried.

if I put a label I get an error saying it requires that 'Label<Text, Image>' conform to 'StringProtocol'

Again, I do not understand what you did. Please show by code what you have tried.
Anyway, Label here is just a placeholder used as a generic parameter. It has nothing to do with SwiftUI.Label.

for me it says that label conform to view... ?

True, the type used as Label, needs to conform to View.

is there a documentation to help me read documentation :-)

Have you ever read the Swift book?
If not yet, I recommend you to read the section of Generics carefully.

Thank you for your help.
you are right about Identifier, I should use an uppercase L when I write an identifier like Label.

I have used generic in the past, but I will go back to try read that part in Apple "the Swift programming language, version 5.3 "

I am currently doing the CP193P from Stanford with Paul Hegarty based on the first SwiftUI
I do his class in part almost every year, this has to do with my new brain condition.
but in parallel I listen to 2020 wwdc video

I wrote a long text with all code I tried and the result I got, but while writing this I think I have understand something.
I will do some test, reading and come back with probably a better question.

Thank you again

I am currently doing the CP193P from Stanford with Paul Hegarty based on the first SwiftUI 

Thanks for sharing your background.
When you write a SwiftUI code, the details of generics are not needed.
You can re-learn generics alongside with (or after) learning how to write SwiftUI apps.
(But I recommend you to read the Swift book thoroughly before starting the first Swift project.)

Good luck and happy coding.
hello OOPer

here is what I tried so far to try to understand how it work

_______________________________________________________
Documentation
Code Block
init<S>(S, content: () -> Content)

Creates a disclosure group, using a provided string to create a text view for the label.
Available when Label is Text and Content conforms to View.


_______________________________________________________
and in the Definition :

Code Block
public struct DisclosureGroup<Label, Content> : View where Label : View, Content : View {

    /// Creates a disclosure group with the given label and content views.
    /// - Parameters:
    ///   - content: The content shown when the disclosure group expands.
    ///   - label: A view that describes the content of the disclosure group.
Code Block
    public init(@ViewBuilder content: @escaping () -> Content, @ViewBuilder label: () -> Label)


    /// Creates a disclosure group with the given label and content views, and
    /// a binding to the expansion state (expanded or collapsed).
    /// - Parameters:
    ///   - isExpanded: A binding to a Boolean value that determines the group's
    ///    expansion state (expanded or collapsed).
    ///   - content: The content shown when the disclosure group expands.
    ///   - label: A view that describes the content of the disclosure group.
Code Block
    public init(isExpanded: Binding<Bool>, @ViewBuilder content: @escaping () -> Content, @ViewBuilder label: () -> Label)


    /// The content and behavior of the view.
Code Block
    public var body: some View { get }


    /// The type of view representing the body of this view.

    /// When you create a custom view, Swift infers this type from your
    /// implementation of the required body property.
Code Block
    public typealias Body = some View
}



_______________________________________________________
from this
struct DisclosureGroup<Label, Content> : View where Label : View, Content : View
I expect to call disclosure with 2 view. but it does not seems to work.
I decide to do some test with the first parameter as a string, because I knew it work and try to give the content for the 2 parameter

Test 2 - I tried this and it work
Code Block
struct ContentView2: View {
    var body: some View {
        DisclosureGroup("Lightning") {
            Text("Fast")
        }
    }
}


Test 3: I tried to enter the Text view into the disclosure calling , did not work
but the content should be () -> Content and Text is taking an argument ...
Code Block
struct ContentView3: View {
    var body: some View {
        DisclosureGroup("Lightning", content: Text("Fast") )
    }
}


Test 4 with the last argument inside - calling a var of type some View - did not work
Code Block
struct ContentView4: View {
    var body: some View {
        DisclosureGroup ("Lightning", content: ContentViewSmall1)
    }
}
struct ContentViewSmall1: View {
    var body: some View {
        Text("ContentViewSmall 1")
    }
}


Test 5 with the last argument inside - calling a func that return some View - Worked
Code Block
struct ContentView5: View {
    var body: some View {
        DisclosureGroup ("Lightning", content: ContentViewSmall2)
    }
}
func ContentViewSmall2() -> some View {
    return Text("ContentViewSmall 2 ")
}



I know I am doing something wrong, and mostly I have an error in my thinking.
if you can help me, it would be great , thank you


from this 
struct DisclosureGroup<Label, Content> : View where Label : View, Content : View
I expect to call disclosure with 2 view. but it does not seems to work. 

Have you really read the Swift book? call disclosure does not make sense.
Label and Content are the generic parameters, they are just a template and replaced to the actual types depending on the context.


Test 2 - I tried this and it work

You are calling the following initializer in this case:
Code Block
    public init(_ titleKey: LocalizedStringKey, @ViewBuilder content: @escaping () -> Content)

It works because this initializer is defined. Not because generic parameters.


Test 3: I tried to enter the Text view into the disclosure calling , did not work 
but the content should be () -> Content and Text is taking an argument ...

As the error message is clearly stating, content needs to be a closure.

Code Block
struct ContentView3: View {
var body: some View {
DisclosureGroup("Lightning", content: {Text("Fast")} )
}
}


Seems you do not understand the difference between a closure returning Text and Text itself.
I recommend you to read the Swift book carefully.


Test 4 with the last argument inside - calling a var of type some View - did not work

There is no initializer in DisclosureGroup taking type name, nor any other Swift types.
I recommend you to read the Swift book carefully.


Test 5 with the last argument inside - calling a func that return some View - Worked 

In Swift, function names can be used as a closure.

The absolutely best documentation I have found (IMHO) is an app called SwiftUI Companion. Pretty complete, and has working examples of almost everything.

It has vastly improved my learning by showing me now only syntax but HOW to use it in real examples.

It is available in both the App Store and on the creators site - which also has a lot of useful blog posts that do deep dives into SwiftUI.

It does cost - but you will not regret the purchase.
Thank you for taking the time to help me. I know I am slow to learn since my brain problem.

I dont get an alert when there is a new post. we use to have a button for that.

Test 3: I tried to enter the Text view into the disclosure calling , did not work 
but the content should be () -> Content and Text is taking an argument ...
As the error message is clearly stating, content needs to be a closure.
struct ContentView3: View { var body: some View { DisclosureGroup("Lightning", content: {Text("Fast")} ) }

ok, I see it now.

What I meant by Enter the Text View, it is because I thought that it was a case where the last argument is a closure and it can be taking out , so I was trying to put it back in


call disclosure does not make sense.

not sure how to name it, it is a struct of type View that need parameter when I created it, and I guess I call the init when I create it ?


Have you really read the Swift book? 
I was up to generic, before posting I am going slowly
I did learn generic in school in a C sharp class but I was 54 returning to school, and my brain was already damaged.

I am old :-) I started way back on paper terminal and then on punch card :-)
I did 100 days of swiftui of Paul Hudson at HackingWithSwift.
and every year (when available) I follow the CS193P of Paul Hegarty of Stanford.

I have an app that I use for myself, that get data via bluetooth from my solar eBike, do some calculation ,display all the results and save it to core data . I want to convert it to swiftui but I see that I need a lot more preparation.

reading is very difficult for me now , strangely a lot more than writing. Discussion is easier.
but I dont want to impose.

I will look into finding a mentor on the web.

thank you


I guess I call the init when I create it ?

True. You call init when creating a struct.
(You may need to find the documentation of init. Not the header of the type.)

I am old :-) I started way back on paper terminal and then on punch card :-) 

Same as me. Maybe I am older than you but I don't think I'm too old to learn Swift.
One advice, forget about C Sharp. Generics in C sharp and generics in Swift are different.

Another advice, you should better include the chapter name/section name of the Swift book (especially in case you do not understand the descriptions). That may help both readers who want to answer to your question and yourself.



@franc
could you send me a screenshot of what it look like for DisclosureGroup , to see if it could help me
then I could buy it. my goal is to learn to read API documentation, so I could follow as everything evolve.
thank you
@OOPer
thank you
old is not the problem :-) learning a language was kind of easy because I have learn so many variant of basically the same thing :-)

and even with a good brain, I think. my problem is with api documentation like we discuss earlier

going from oop to declarative, is also a bit difficult. I managed to create an infinite loop when SwiftUI was announce :-)

I remember fortran on Punch card where we had to put a certain character in column 6 or 7
(6 it seems after a web search)
now in SwiftUI there are some times where I feel the same ways, I am not sure where to put stuff in Body.

but I loved it so I will get it... one day.

my problem is with api documentation like we discuss earlier 

In fact, the documentations of SwitfUI are far worse than poor. Maybe some words which are excluded from this site would be appropriate. I really wonder why Apple's engineers (not Document engineer, but the designers and the implementers) are not interested in writing a good documentation.

But one reason you cannot read them well is that you assume things where it is not expected.
You first assumed <Label, Content> would define the parameters of an initializer, but it does not.
And even in C sharp, generic parameters does not define the parameters of a constructor.

I do agree that SwiftUI forces us some different aspects than that of procedural programming, but there are many of such things waiting for you and stopping on a type header does not seem to be a good attitude.

Anyway, check the documentation of the initializer when you want to call init of a struct.