Creating a class or structure
We use the same syntax to define classes and structures. The only difference is that we define a class using the class
keyword and a structure using the struct
keyword.
Let's look at the syntax that's used to create both classes and structures:
class MyClass {
// MyClass definition
}
struct MyStruct {
// MyStruct definition
}
In the preceding code, we define a new class named MyClass
and a new structure named MyStruct
. This effectively creates two new Swift types, named MyClass
and MyStruct
. When we name a new type, we want to use the standard naming convention set by Swift, where the name is in camel case, with the first letter being uppercase. This is also known as PascalCase
. Any method or property defined within the class or structure should also be named using camel case, with the first letter being uppercase. Empty classes and structures are not that useful, so let's look at how we can add properties to our classes...