define a class with a first name, last name and email properties in swift

main.swift
class Person {
    var firstName: String
    var lastName: String
    var email: String
    
    init(firstName: String, lastName: String, email: String) {
        self.firstName = firstName
        self.lastName = lastName
        self.email = email
    }
}
259 chars
12 lines

In the example above, we use the class keyword to define a new class called Person. The class has three properties: firstName, lastName, and email, which are of type String. To create a new instance of this class, we define an init method that takes three arguments: firstName, lastName, and email. In the implementation of the init method, we assign these arguments to the corresponding properties of the new instance using self.

gistlibby LogSnag