Swift, Apple's programming language for building iOS applications. Swift is also the latest programming language for iOS and OS X apps that build on the best of C and Objective-C, without the constraints of C compatibility. Swift adopts secure programming patterns and adds modern features to make programming easier, more flexible, and more fun.
Learn Swift Programming in 2021
Learn Swift Programming |
Why Learn Swift?
Object-Oriented Programming in Swift.
Overview
Object-oriented pre-programming (OOP) is a programming paradigm that represents the concept of “objects” that have data fields (attributes that describe the object) and associated procedures known as methods. Objects, which usually instances of classes, are used to interact with one another to design applications & computer programs.
There are 3 key aspects of object-oriented programming:
Encapsulation means objects keep their state information private. Rather than directly manipulating an object’s data, other objects send requests to the object, in the state messages, some of which the object may respond to by altering its internal state.
Polymorphism means that objects of different classes can be used interchangeably. This is especially most important, as it allows you to hook up classes at later date in ways you didn’t necessarily foresee when those classes were first designed.
Inheritance means that objects of one class can derive part of their behavior from another (base or parent) class. Some object-oriented languages (C++, for example, but not Swift) allow multiple inheritances, where objects of one class can derive part or all of their behavior from multiple independent base classes.
Classes and Objects
In object-oriented programming, a class is an extensible to program-code template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions, methods). In other words, a class is like a blueprint, it defines the data and behavior of a type.
The definition above creates an empty class named Button
. The only thing it can do is create new Button
objects:
In the example above button
is an instance of the Button
class.
Properties
Classes and instances can have associated values named properties.
The Square
class has a length
property that has a default value of 1
.
In order to create squares with a different length
than 1
we need to write a custom initializer.
Methods
Methods add behavior to classes and instances.
The area()
the method computes the area of a Square
instance when called. To call a method using the notation.
Class Properties
The usual use of properties is instance properties, like the length of a Square. You can also have properties. Below is an example use of class properties. The Tank
the class has a computed property named bonusDamage
that is used to increase the damage for all tanks when upgrading. When an upgrade is complete all we need to do is callTank.upgrade()
and all Tank
instances will have more damage.
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, and the class it inherits from is known as its superclass
. Inheritance is a fundamental behavior that differentiates classes from other types in Swift.
A simple example of inheritance:
Overriding
You can override methods in order to provide custom behavior. To override a method write the override
the keyword before the method declaration:
You can use the super
keyword to call any method from the superclass.
Base class
A class that does not inherit from another class is called a base class
, for example:
The iOS and Mac OS classes usually inherit from NSObject, either directly or indirectly. If you have a mixt codebase I would encourage you to subclass NSObject
when creating a new class:
Swift classes that are subclasses of NSObject:
- are Objective-C classes themselves
- use
objc_msgSend()
for calls to (most of) their methods- provide Objective-C runtime metadata for (most of) their method implementations
Swift classes that are not subclasses of NSObject:
- are Objective-C classes, but implement only a handful of methods for NSObject compatibility
- do not use
objc_msgSend()
for calls to their methods (by default)- do not provide Objective-C runtime metadata for their method implementations (by default)
Subclassing NSObject in Swift gets you Objective-C runtime flexibility but also Objective-C performance. Avoiding NSObject can improve performance if you don’t need Objective-C’s flexibility.
from stackoverflow Swift native base class or NSObject
Protocols
Protocols are declared in a similar way to classes.
Protocols describe methods, properties, and other requirements that are needed for a specific task. For example, the protocol lists all the methods that can be used to react to user events and configure a table view.
Note: you can mark a method as optional using the @optional
keyword. All of the methods fromUITableViewDelegate
are optional. When you do not use the @optional
keyword that method is required. The swift compiler will throw an error if a class conforms to a protocol and does not implement the required methods.
A class can conform to a protocol by placing its name after the type’s name separated by a colon, as part of its definition. Multiple protocols can be listed, and are separated by commas:
If the class inherits from another one, make sure to put the superclass name before the protocol list.
Note: Protocols use the same syntax as normal methods, but are not allowed to specify default values for method parameters.
Delegate Pattern
One of the most overused design patterns in iOS is delegation. A class can delegate a part of its responsibilities to an instance of another class. This design pattern is implemented by defining a protocol that encapsulates the delegated responsibilities, such that a conforming type (known as a delegate) is guaranteed to provide the functionality that has been delegated.
Here are an example, the Player class delegates the shooting logic to the weapon:
Polymorphism
Polymorphism means “having multiple forms”. Objects of different classes can be used interchangeably if they have a common superclass
.
Here is a simple example in which multiple instances can be used as a GraphicObject
.
This program will output:
Singleton Pattern
Sometimes it’s important to have only one instance for a class. For example, in a system, there should be only one window manager (or only a file system, or one motion manager on an iPhone). To achieve this effect in swift we are going to expose the singleton instance using a class property.
or:
Model View Controller
The best explanation I found was from Apple:
The Model-View-Controller (MVC) design pattern assigns objects in an application one of three roles: model, view, or controller. The pattern defines not only the roles objects play in the application, it defines the way objects communicate with each other. Each of the three types of objects is separated from the others by abstract boundaries and communicates with objects of the other types across those boundaries. The collection of objects of a certain MVC type in an application is sometimes referred to as a layer—for example, a model layer.
MVC is central to a good design for a Cocoa application. The benefits of adopting this pattern are numerous. Many objects in these applications tend to be more reusable, and their interfaces tend to be better defined. Applications having an MVC design are also more easily extensible than other applications. Moreover, many Cocoa technologies and architectures are based on MVC and require that your custom objects play one of the MVC ro
Model Objects
Model objects encapsulate the data specific to an application and define the logic and computation that manipulate and process that data. For example, a model object might represent a character in a game or a contact in an address book. A model object can have to-one and to-many relationships with other model objects, and so sometimes the model layer of an application effectively is one or more object graphs. Much of the data that is part of the persistent state of the application (whether that persistent state is stored in files or databases) should reside in the model objects after the data is loaded into the application. Because model objects represent knowledge and expertise related to a specific problem domain, they can be reused in similar problem domains. Ideally, a model object should have no explicit connection to the view objects that present its data and allow users to edit that data—it should not be concerned with user interface and presentation issues.
Communication: User actions in the view layer that create or modify data are communicated through a controller object and result in the creation or updating of a model object. When a model object changes (for example, new data is received over a network connection), it notifies a controller object, which updates the appropriate view objects.
View Objects
A view object is an object in an application that users can see. A view object knows how to draw itself and can respond to user actions. A major purpose of view objects is to display data from the application’s model objects and to enable the editing of that data. Despite this, view objects are typically decoupled from model objects in an MVC application.
Because you typically reuse and reconfigure them, view objects provide consistency between applications. Both the UIKit and AppKit frameworks provide collections of view classes, and Interface Builder offers dozens of view objects in its Library.
Communication: View objects learn about changes in model data through the application’s controller objects and communicate user-initiated changes—for example, text entered in a text field—through controller objects to an application’s model objects.
Controller Objects
A controller object acts as an intermediary between one or more of an application’s view objects and one or more of its model objects. Controller objects are thus a conduit through which view objects learn about changes in model objects and vice versa. Controller objects can also perform setup and coordinating tasks for an application and manage the life cycles of other objects.
Communication: A controller object interprets user actions made in view objects and communicates new or changed data to the model layer. When model objects change, a controller object communicates that new model data to the view objects so that they can display it.
Challenges
- create a
Shape
base class and derive from itCircle
,Square
andRectangle
.Shape
should have two methodsarea() -> Float
andperimeter() -> Float
that both return 0. Implement the methods and add the necessary properties onCircle
,Square
andRectangle
. - create the same classes from the first exercise, but this time make
Shape
a protocol - create a custom initializer for
Circle
,Square
andRectangle
and create two instances of each one. - add the instance created in the exercise above into an array
shapes
and use polymorphism to find out the total area and perimeter of the shapes - solve the exercise above using map and reduce
- add a
armor
property to theTargetable
protocol and reduce the damage taken by the enemy. Obs: you will need to change thelife
property type toFloat
in order to do this.