Share

Share











Welcome future Swift developers to the first lesson in the Swift Series.

I am going to explain some of the basics when it comes to creating data models using Swift programming and syntax. For those newer to programming, Swift is an object-oriented programming language. What that means in my own definition is; is that everything that is typed or coded, is meant to be thought of as an object or referring to an Object.

Now lets talk about the most common object in not only Swift, but the entire programming community.

Classes

A class, when defined in Swift, is the model that acts as a set of instructions, or the “blueprints” on how to create an object. When you initiate a new class object, each instance is created by “reference” to the original class. We will go over reference type objects vs value types more in-depth in the second part of this lesson.

Let’s say that we want to create a Fruit object. To do that, we need to define properties that make up what a Fruit object would be. Properties are the values and conditions associated with an object.

Here is an example of a Fruit class declared in Swift.

The name and color are properties of the fruit object.

When you create an object in Swift, you have to make sure it can be properly initialized.

In order for our fruit object to be initialized, we give it a name and color inside the initializer function.

So lets create some Fruit.

Now we have a banana, a strawberry, and a grannyApple object initialized and ready to play around with. The reason I keep using the word initialized, is because in programming initialized roughly can mean creating. To say that an object was initialized in a Swift playground is a more accurate way to say that an object was created in Swift playgrounds.

Every class has properties, and most of the time when you create a class, you need to give or input, some data in order to fulfill required properties.

The values you input to the class at initialization are handled in an init function.

IF you remembered from above, we need a fruit name and a fruit color to be declared in order to properly initialize a Fruit class. I decided to name these outside properties fruitName and fruitColor respectively. Then I set the class property ‘name’ to fruitName, and ‘color’ to fruitColor.

init (fruitName: String, fruitColor: String) { self.name = fruitName self.color = fruitColor }

So to recap, When we create our Fruit object, we need to give it a fruitName of type String and a fruitColor of type String to make a Fruit Object. Of type String is a class used to represent characters that are typed; a ‘string of characters’, or ‘characters stringed together’.

Once again, inside the init function, is when we handle outside properties before they get set to the actual class object. If we wanted to manipulate the values before we set the internal properties, this is where we would do it. For Styling purposes, lets make sure that every fruit name is capitalized.

(.capitalized is a function that makes sure whatever value we pass in, gets capitalized when we set fruit.)

Playing With Created Data

With out fruit objects fully initialized, we can play around with that data that makes them up.

In the picture above, I called the print method on the strawberry object, and the compiler printed out the value ‘ Strawberry ‘.

Lets say we want an easy way to print out all the names of the different fruit weve declared. One way to do this is by creating an array of fruit, and then telling the compiler to print out the name of each object in the array.

(An array is a way to represent multiple objects.)

For each fruit in the newly created fruit array, print the fruit name. And the compiler spits out each of the names.

Custom Classes

Now lets get a little fancy. Were going to create a person class, with a name, age, a favorite fruit.

If you’ve noticed, the property for age is in months. The init function used to set the months is wanting the age in years. A lots of times you need to manipulate the data given from the initialized inputs, before you set the properties associated back to its self.

(Int type is a class used to represent numbers as an integer. It allows for the compiler to apply math easily)

Lets create a couple people objects and group them into an array called people

Now I want a way for each person object to print out and describe what their name, favorite color, and age is.

An excellent way to do that, would be create a function on the class itself that does that. Create an array of the new people objects, then loop through the array , and call the function.

Now after the compiler runs, at the bottom we can we the descriptions. As you can see, the description is not formatted in a way that is legible. There is no punctuation, and the age in months is not how we communicate age to each other, except when referring to a baby.

In the describeSelf() function, we can do a couple things to help with the outputs user readability.

First, the ageInMonths property is of integer (Int) type, and that is a problem because we want to use a print function.The print() function requires all inputs to be of type String. Lucky for us, swift added a shorthand expression “\()” , to help easily convert non-String types into a String on the same line of code.

We should add a string to help describe that this is age displayed in months, and that the fruit is the persons favorite fruit.

Lastly, one more thing we could add to help make this function describe better, would be to change the age which is represented in months, to be display in years.

With Swift, this simple math can be done easilyinside the print statement

Wrap-up

Class objects are the most fundamental pieces to swift programming. Almost every single object you are going to create in Swift, will be inherited from a super class.

Next week I will going over the types of data model types that are not classes, how they pertain to iOS development, and how and when to implement them to help you create a powerful and robust code base.

Keep Swifting,

Neels McGee