Group Group Group Group Group Group Group Group Group

Not fully understanding structs, code, and constructors in views

Let me give an example …

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(taskStore: TaskStore() )
}
}

Here we have a structure of type PreviewProvider. It has a variable called previews. I’m not sure what “some View” means, but it appears to be the type. Inside the variable is what appears to be a function call. (However, that function call is not made inside a code block. There is no function declared within the structure to be making function calls.) However, that function call is to a data type “ContentView”. Am I correct in assuming that it is calling the constructor for that data type? (Shouldn’t the result of the constructor call be assigned to a variable?) Likewise, it is passing a variable called taskStore, which appears to be created by calling another constructor. However, I don’t see constructors defined for either one of these things. It is just like making the fucnction calls creates the objects?


Best wishes,

John Coffey

http://www.entertainmentjourney.com

Hi @john2001plus,
There is a lot of internal wiring that happens with SwiftUI.

A View is a generic term for things like Text, Button, etc. So instead of specifically returning a type of view, Swift offers a way of returning any type of View.

When we create a structure of type PreviewProvided, it has a static variable called previews, this needs to return a type of view as explained above.

In the preview the function ContentView would return the view and it takes a parameter which is returned from initialization of TaskStore. Look at the declaration of the ContentView.

You might want to read a bit more on ‘some’.

Getting to the heart of my question… It is hard to distinguish between variable declaration and function calls. ContentView(taskStore: TaskStore()) looks like a function call, but it might just be declaring a structure called ContentView. You say it is a function call, so I take your word for it, but what function is it calling? ContentView is a structure and not a function, unless this is calling the constructor to ContentView??? Inside structure definitions, I see what look like code statements, but those code statements aren’t inside a function or method where they would normally be in a language like C++. This is where I get confused because the syntax seems ambigous to me. How can a variable like previews have a return value?

Please correct me if I am wrong, which I very well could be, but in the example way below, ContentView(taskStore: TaskStore() ) is most likely a function call, which returns the “computed value” for the variables “previews”. The problem here is that if I look at the structure of “ConentView” there is no function called “ContentView”. Am I calling an implied constructor? This syntax is confusing the heck out of me.

Even though you referred to “ContentView(taskStore: TaskStore() )” as a function call, which is what I have been wondering about, this seems to me to not make sense. It appears to me that this line is just declaring an instance of the structure “ContentView”, and passing in a value for taskStore, which is another structure which is being created.

If I am wrong, please tell me.

Hi @john2001plus,
your questions are quite valid and understandable from a beginners perspective.

Consider this,

func giveMeFive() -> Int {
    return 5
}

That is a simple function, are you familiar with that? It takes no parameters but returns a result of type Int.

Similarly, say we had a function called giveMeAView and described as

func giveMeAView() -> View {
    return View()
}

right now these are simple functions, however they could end up returning a view based on some complex set of criteria.

As to the heart of your question, have you read or heard about initializers? ContentView() is an initializer, that is a function that is called when creating an instance of the structure or class.

A method or a function is defined as functionName followed by brackets. Even an initializer is a function that is declared as init() { ... }

cheers,

and for your second part, how can a variable have a return value, it is called a computed variable, where instead of applying a static/constant value to it, you can run a function and assign the result as the value.

Thanks for the help.