maandag 4 april 2016

The use of the companies class in the Marineterrein app.

My fellow student and I are building an app for the Marineterrein. Many companies are established on a big area that is called Marineterrein and is situated in Amsterdam. To get more collaboration between the companies on the Marineterrein the idea was raised to create an app. Part of the app is the ability to search in a register of companies on the Marineterrein. 

In the register data will be stored like, name of the company, telephone, address, location, logo and telephone number. And some other info as well. These are known as properties in Swift. In the app a separate file is created. The file is called Companies.swift. In the file a class is created. The class is called Companies. It is a class in which the data is initiated which should be available in the app. 

As said the class Companies has properties. A class can have stored properties, which can either be constants or variables. One of the main features of classes is inheritance: a class can inherit methods, properties, and other characteristics from another class. When one class inherits from another, the inheriting class is known as a subclass, and the class it inherits from is known as its superclass. Also a class can be used as a blueprint of an object: a drawing of a building for instance. Classes are also reference types. They have their own unique identity.

The first part of the class of the app looks like this:

class Companies: NSObject {
    var nameCompany: String
    var address: String
    var phoneCompany: Int
    var emailCompany: String
    var imageCompany: UIImage?
    var url: NSURL
    var entranceCode: String

The class Companies inherits from NSObject. This is a called a root class: it provides basic support for Objective-C runtime. NSObject is the base class that most Objective-C classes inherit from. All of the UIKit classes like UIView, UITextField and UIViewController inherit directly or indirectly from NSObject. NSObject is needed of your class (ie Companies) need to interface with the runtime system. In our case it is the URL of the website of the company that is stored. A URL is of type NSURL in Swift and therefore should the class inherit from NSObject.

For every property it is indicated which data type it should have. String is a range of characters, Int is a whole number, UIImage is a specific data type for images and NSURL is  a specific one for the url variable.

After the declaration of the blueprint all the properties are initiated. In a class the properties have to be initialized. The properties have to have a first value. Below the first part. Here the parameters of the initializer are defined.
init(nameCompany: String, address: String, phoneCompany: Int, emailCompany: String, imageCompany: UIImage, url: NSURL, entranceCode: String) 

After the initialization the properties are set to their real value. 

{
self.nameCompany = nameCompany
      self.address = address
      self.phoneCompany = phoneCompany
      self.emailCompany = emailCompany
      self.imageCompany = imageCompany
      self.url = url
      self.entranceCode = entranceCode
}
}

In above lines it says that the property (self.nameCompany) is the same value as the parameter of the initializer.


Now the class is set up and ready to use. All company records will have the same properties (read details) to make the register complete.