Create a New Class in Swift

In this tutorial, you will learn how to create a new class in Swift.

After learning the basics of creating classes in Swift, you can dive deeper and learn how to implement a Singleton pattern in Swift, and create a class that has only one instance throughout your project.

Alright, so let’s begin by declaring an empty class first.

Declare an Empty Class in Swift

Below is a code example that declares a simple class in Swift without any properties or methods.

class Person {

}

let person = Person()

The class Person {} code snippet defines an empty class named Person. Even though this class has no properties or methods, you can still create an instance of it using let person = Person(). The Person() is the default initializer provided by Swift to create a new instance of the Person class.

A Class in Swift with One Initializer

Now, let’s add an initializer to our class to set some initial values for its properties.

class Person {
    var name: String
    var age:Int
    
    init(age: Int, name: String) {
        self.age = age
        self.name = name
    }
}

let person = Person(age: 30, name: "Sergey")

The init(age: Int, name: String) method is an initializer that sets initial values for the name and age properties when a Person object is created. The line let person = Person(age: 30, name: "Sergey") creates a new Person object with initial values.

A New Class with Multiple Initializers

In this section, I will show you how to declare a new class with two initializers.

class Person {
    var firstName: String
    var lastName: String
    var email: String?

    init(firstName: String, lastName: String) {
        self.firstName = firstName
        self.lastName = lastName
    }

    init(firstName: String, lastName: String, email: String) {
        self.firstName = firstName
        self.lastName = lastName
        self.email = email
    }
}

let person1 = Person(firstName: "Sergey", lastName: "Kargopolov")
let person2 = Person(firstName: "Sergey", lastName: "Kargopolov", email: "sergey@example.com")

In this code example, I have defined a Person class with two initializers. The first initializer accepts two arguments for firstName and lastName, while the second initializer accepts an additional argument for email. This way, you can create a Person instance either with or without an email address.

A Class with a Method

In this section, we’ll create a new class in Swift, and we will add a method that returns a string.

class Person {
    var firstName: String
    var lastName: String
    
    init(firstName: String, lastName: String) {
        self.firstName = firstName
        self.lastName = lastName
    }
    
    func getFullName() -> String {
        return firstName + " " + lastName
    }
}

let person = Person(firstName: "Sergey", lastName: "Kargopolov")
print("Full name: " + person.getFullName())

The func getFullName() -> String method is defined within the Person class to return the full name of a person. The getFullName method concatenates the firstName and lastName properties with a space in between, and returns the result.

Conclusion

You’ve now learned how to create a class in Swift, along with adding initializers and defining methods. There’s much more to explore in Swift programming. Check out other Swift code examples to further enhance your understanding and skills.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *