Structure type property - How to create a type property of same type as the structure itself?

Hi.

I am learning Swift from the book called Develop In Swift Fundamentals and I am doing the exercises about Structures. I thought I have a good understanding of Structures, but an exercise totally confuses me right now and I would like to ask fro some help with understanding the task and the solution to this exercise.

So, in the exercise, we have a struct:

struct User {

var userName: String

var email: String

var age: Int

}

And this is the task:

Create a currentUser type property on the User struct below and assign it to a user object representing you. Now you can access the current user through the User struct.

I am not a native English speaker and I just don't understand the task the exercise want me to do.
So it is want me to create a type property called currentUser. It is fine. What is confusing me is the rest of the sentence, it is telling me the property to be type of User?
Like static var currentUser: User?
But it does not compile, I am not allowed to do this.

So then I figured oh well, maybe it is just meant to create an instance of type User and call it currentUser but then here is the second task.

Go back and create a type method called logIn(user:) where user is of type User. In the body of the method, assign the passed in user to the currentUser property

So this tells me, no it did not meant to be an instance, it does actually mean to create a type property of User Type. But when I do that, I get an error saying, "It is not allowed".

Could anyone please help me with the solution here? It is really bothering me that I don't understand the task and I'm afraid maybe I am missing something important here that I should know at this point but I don't know what it is and I don't understand the task.

I would really appreciate any help here.
Thanks.

struct User {
  var userName: String
  var email: String
  var age: Int

  static var currentUser: User { fatalError("Not implemented") }
}

You seem to understand the task correctly. The code above compiles without any problem. There is no error.

What is the error that you are getting?

'static var' declaration requires and initialiser expression or getter/setter specifier.

Now I can see you did pass fatalError() to the object and yes it does compile, but when I try to assign a user object to it, I get the error currentUser is a Get only property

At this point the book did not teach anything about getters and setters.

Thanks.

This is not your fault. It is the fault of your book. You will need to learn how to create a getter or to write an initializer expression in order to do the task described. If the book has not taught you these things yet, then you are not ready to perform this task.

1 Like

This error shows up if you do not provide a value to a static property (or ways to get/set a value), since there's no way to initialize that property otherwise. Unlike instance properties, a type property cannot be initialized in an init.

You don't need to know how getters work in Swift in order to complete that exercise. The way @xwu replied involves a getter: in general the expanded notation is this

var propertyName: PropertyType {
  set { /* do something */ }
  get { /* return an instance of PropertyType */ }
}

If you only need a getter, you can use a more compact notation:

var propertyName: PropertyType {
  /* return an instance of PropertyType */
}

Regarding your exercise, you understood the task correctly. You need to define a static variable called currentUser, initializing it with a User instance.

struct User {
  var userName: String
  var email: String
  var age: Int
  
  static var currentUser = User(
    userName: "username",
    email: "email",
    age: 0
  )
  
  static func logIn(user: User) {
    User.currentUser = user
  }
}

User.logIn(user: User(userName: "username1", email: "email1", age: 1))
1 Like

Thanks a lot for the explanation, it is really helped :slight_smile: