To create a class in Swift, you first need to declare it using the class
keyword followed by the class name. After that, you can define properties and methods inside the class.
main.swift59 chars4 lines
Here's an example for creating a class Person
with two properties name
and age
:
main.swift238 chars14 lines
In the above example, we have created a class Person
with two properties name
and age
, and a method sayHello
that prints a greeting message. The init
method is the initializer for the class that takes in two parameters name
and age
.
gistlibby LogSnag