# Not fully understanding structs, code, and constructors in views

**URL:** <https://forums.kodeco.com/t/not-fully-understanding-structs-code-and-constructors-in-views/118974>\
**Category:** Swift\
**Created:** [September 1, 2020, 10:22pm UTC](https://forums.kodeco.com/t/not-fully-understanding-structs-code-and-constructors-in-views/118974 "2020-09-01T22:22:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![john2001plus](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/john2001plus/32/81494_2.png) [@john2001plus](https://forums.kodeco.com/u/john2001plus)\
**Post date:** [September 1, 2020, 10:22pm UTC](https://forums.kodeco.com/t/not-fully-understanding-structs-code-and-constructors-in-views/118974/1 "2020-09-01T22:22:12Z")

</div>

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](http://www.entertainmentjourney.com)

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [September 12, 2020, 9:35am UTC](https://forums.kodeco.com/t/not-fully-understanding-structs-code-and-constructors-in-views/118974/2 "2020-09-12T09:35:00Z")

</div>

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’.

---

<div class="post-metadata">

**Author:** ![john2001plus](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/john2001plus/32/81494_2.png) [@john2001plus](https://forums.kodeco.com/u/john2001plus)\
**Post date:** [September 19, 2020, 5:59pm UTC](https://forums.kodeco.com/t/not-fully-understanding-structs-code-and-constructors-in-views/118974/3 "2020-09-19T17:59:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![john2001plus](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/john2001plus/32/81494_2.png) [@john2001plus](https://forums.kodeco.com/u/john2001plus)\
**Post date:** [September 19, 2020, 8:42pm UTC](https://forums.kodeco.com/t/not-fully-understanding-structs-code-and-constructors-in-views/118974/4 "2020-09-19T20:42:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![john2001plus](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/john2001plus/32/81494_2.png) [@john2001plus](https://forums.kodeco.com/u/john2001plus)\
**Post date:** [September 19, 2020, 9:17pm UTC](https://forums.kodeco.com/t/not-fully-understanding-structs-code-and-constructors-in-views/118974/5 "2020-09-19T21:17:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [September 21, 2020, 5:06am UTC](https://forums.kodeco.com/t/not-fully-understanding-structs-code-and-constructors-in-views/118974/6 "2020-09-21T05:06:57Z")

</div>

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

Consider this,

```auto
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

```auto
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,

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [September 21, 2020, 5:08am UTC](https://forums.kodeco.com/t/not-fully-understanding-structs-code-and-constructors-in-views/118974/7 "2020-09-21T05:08:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![john2001plus](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/john2001plus/32/81494_2.png) [@john2001plus](https://forums.kodeco.com/u/john2001plus)\
**Post date:** [October 21, 2020, 6:27pm UTC](https://forums.kodeco.com/t/not-fully-understanding-structs-code-and-constructors-in-views/118974/8 "2020-10-21T18:27:29Z")

</div>

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [February 15, 2021, 2:22pm UTC](https://forums.kodeco.com/t/not-fully-understanding-structs-code-and-constructors-in-views/118974/9 "2021-02-15T14:22:13Z")

</div>

This topic was automatically closed after 166 days. New replies are no longer allowed.
