Object Oriented Programming in Swift

Author: Info Campus

The vast majority of Apple's systems have a protest situated design. Before you begin diving into iOS/MacOS improvement you should first comprehend question situated programming and configuration designs. In this article we will experience the fundamental thoughts and configuration designs keeping in mind the end goal to kick you off with application improvement.

Question arranged programming (OOP) is a programming worldview that speaks to the idea of "objects" that have information fields (traits that depict the protest) and related methodology known as techniques. Items, which are normally occurrences of classes, are utilized to cooperate with each other to plan applications and PC programs.

There are 3 key parts of question arranged programming:

Epitome implies that items keep their state data private. Instead of straightforwardly controlling a question's information, different items send solicitations to the protest, as messages, some of which the protest may react to by modifying its inside state.

Polymorphism implies that objects of various classes can be utilized reciprocally. This is particularly vital, as it enables you to connect classes at a later date in ways you didn't really predict when those classes were first composed.

Legacy implies that objects of one class can determine some portion of their conduct from another (base or parent) class. Some protest situated dialects (C++, for instance, yet not Swift) permit numerous legacy, where objects of one class can infer part or the greater part of their conduct from various autonomous base classes.

Classes and Objects

In protest situated programming, a class is an extensible program-code-layout for making objects, giving introductory esteems to state (part factors) and usage of conduct (part works, strategies). At the end of the day, a class resembles a plan, it characterizes the information and conduct of a sort.

class Button {

}

The definition above makes a vacant class named Button. The main thing it can do is make new Button objects:

var catch = Button()

In the case above catch is an occurrence of the Button class.

Properties

Classes and occurrences can have related esteems named properties.

class

The Square class has a length property that has a default estimation of 1.

Keeping in mind the end goal to make squares with an unexpected length in comparison to 1 we have to compose a custom initializer.

class

}

var firstSquare = Square(length: 3)

println(firstSquare.length)

var secondSquare = Square(length: 10)

println(secondSquare.length)

on the off chance that firstSquare.length Hello from AClass

let enhanced_object = Subclass()

enhanced_object.doSomething()

/> Hello from AClass

Superseding

You can supersede strategies with a specific end goal to give custom conduct. To supersede a strategy compose the abrogate catchphrase before the technique announcement:

class AClass {

func doSomething() {

println("Hello from AClass")

}

}

class Subclass: AClass {

abrogate func doSomething() {

println("Hello from Subclass")

}

}

let base_object = AClass()

base_object.doSomething()

/> Hello from AClass

let enhanced_object = Subclass()

enhanced_object.doSomething()

/> Hello from Subclass

You can utilize the super catchphrase to call any technique from the superclass.

...

class Subclass: AClass {

supersede func doSomething() {

super.doSomething()

println("Hello from Subclass")

}

}

let enhanced_object = Subclass()

enhanced_object.doSomething()

/> Hello from AClass

/> Hello from Subclass

Base class

A class that does not acquire from another class is known as a base class, for instance:

class

}

The iOS and Mac OS classes for the most part acquire from NSObject, either specifically or in a roundabout way. On the off chance that you have a mixt codebase I would urge you to subclass NSObject while making another class.