Log in from a computer to take this course

You'll need to log in from a computer to start Learn Swift. But you can practice or keep up your coding streak with the Codecademy Go app. Download the app to get started.

apple storegoogle store
Learn

Just like structures, we can use classes to model everyday objects. And the syntax looks awfully similar.

Here’s the basic syntax for creating a class:

class Name {

}

The class keyword followed by a class name creates the class. By convention, the class name is capitalized. And just like structures, classes also contain both properties and methods.

Suppose we are writing a program for a school to keep track of all the students. Then we can create a blueprint for this type of data by using a class.

Here’s a Student class that models a student:

class Student { var name: String var year: Int var gpa: Double var honors: Bool }

The Student class has four properties:

  • .name of the type String
  • .year of the type Int
  • .gpa of the type Double
  • .honors of the type Bool

We can also use Swift’s default property values so that the Student class can have predefined values inside the class:

class Student { var name = "" var year = 0 var gpa = 0.0 var honors = false }

Now when we create a new instance of Student, that instance’s .name will be an empty string "", its .year will be 0, and so on.

Instructions

1.

Create a Restaurant class with the following properties:

  • .name with a default value of ""
  • .type with a default value of [""]
  • .rating with a default value of 0.0
  • .delivery with a default value of false

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?