Hello!
In today's post we will be covering the three basic concepts that Objective-C, an object oriented programming language, is made from. Those 3 concepts are:
Object oriented programming languages revolve around the object. The way they work is you create an object first and then create methods or actions that you want to do to the object. Each of these three concepts have tons to do with each other, and failing to make these three concepts work together will make the apps you make fail. So let's get started!
In today's post we will be covering the three basic concepts that Objective-C, an object oriented programming language, is made from. Those 3 concepts are:
- Classes
- Objects
- Methods
Object oriented programming languages revolve around the object. The way they work is you create an object first and then create methods or actions that you want to do to the object. Each of these three concepts have tons to do with each other, and failing to make these three concepts work together will make the apps you make fail. So let's get started!
Overview
As stated before classes, objects, and methods have a lot to do with each other. So, I am going to use a very common analogy to help you understand. Think of cars: convertibles, smart cars, minivans, etc. Well, each type of automobile would be a class. A class is basically a container for the different objects and methods. Think of a class as the factory that creates and does things with those objects which are the types of cars in this case. So in the class you can also specify properties that each object has. Think of lets say minivans. So you have a class called "Minivan." Now, you create an object that is of type "Minivan." Then, you create properties for that Minivan. It has 4 wheels, 7 seats, 6 windows, etc. Now say you are low on gas with this minivan, and you want to fill up the tank. This is where methods come in . You can create methods to do anything to the object. It can fill up the gas, add more wheels, anything. Now, let's dive right into some basic syntax of classes, objects, and methods.
Syntax of Classes
Fire up Xcode. Create a project called Car Factory with the single view application template. This will be just for the iPhone. You do not need to understand what a Storyboard or a View is right now we will cover that later on. Just go into the class called ViewController.m
and delete these lines of code. Now we are going to create a custom class called Minivan. So go up to file and click New -> File.
Then when the pop up appears click Cocoa Touch underneath the iOS section and select Objective-C Class. Then press Next.
Then in the field labeled Class write
Minivan . And in the field that is labeled Subclass of write NSObject . Double click the Car Factory folder.
Leave the bottom as is like this.
Then press Create. Now drag the two files that appear underneath the two ViewController files.
Click
Minivan.m Then write in the init method. Now you have a factory to start making some minivans. (We will go over the init method later on. Right now you just need to understand that the init method is called every time you make an object of type
Minivan )Syntax of Methods
Let's create some methods. We are going to create two methods. First, create a new line underneath the closing curly brace of the init method. Here we will create a method called nameTheCar.
The - in front of the opening parentheses indicates that the method is an instance method. We will go over that when we look at scope. The void inside of the parentheses indicates that this method has no return value. We will also look more into that later. The name of the method is
nameTheCar, and between the two curly braces you will write what the method will do. Then write your second method called fillGasWithThisManyGallons:(int)gallons Congratulations! You just wrote your first two methods!
In the next post, we will go over objects in more depth and then look into data types, arithmetic, and variables using the same Xcode project we used today so don't lose it!
Thank you,
Zachary Cmiel
The PoKoBros
In the next post, we will go over objects in more depth and then look into data types, arithmetic, and variables using the same Xcode project we used today so don't lose it!
Thank you,
Zachary Cmiel
The PoKoBros