Classes and Functions in Swift

What is OOP

Classes and Functions in Swift are important sections of OOP. OOP (Object Oriented Programming) is a fundamental concept of software development. To clarify, OOP is a programming paradigm that can only be defined as contrast to other programming paradigms. In Short, there are 4 main coding paradigms:

  • Functional which view programs as mathematical formulas.
  • Imperative that view a program as a series of instructions.
  • Logical that is a model of information and the relationship between them.
  • OOP where objects represent models and their interactions handles data.

Classes and Functions in Swift in a nutshell

In OOP, we benefit from instructions that can build something. Those instructions are called classes. When we create an instance of that class, we call it an object. Likewise, each class has a set of attributes and methods.

  • Attributes are things that a class has and subsequently their objects have.
  • Methods on the other hand, are functions that instances of a class can do.
OOP – Object Oriented Programming in iOS

Struct in Swift

Structs in Swift are very similar to that of Classes. However, there are two big difference:

  • Firstly, structs cannot have a base class (or a base struct).
  • Secondly, classes pass the reference while, structs pass the value while .

As a result, classes are a lot larger and slower. As a rule of thumb:

If you can get away with structs, use them. If not, use a class.

iOS with Swift and Struct

Initialization in Swift

In classes, we can add multiple different inits. For instance, we might have required or convenience inits. Here are a few things to keep in mind when working with inits:

  • There is a default init() associated with all classes.
  • Inits from super classes can also be overridden.
  • If you have overridden an init, you can no longer use the init().
  • Swift automatically de-allocates your instances when they are no longer needed.
  • Optionally classes may have a deinit method for deinitialization.
Initialization in Swift iOS

Convenience Init

Convenience initializers are supporting initializers to create an instance of that class for a specific use case or input value type. Consequently, we often use convenience inits when we want to populate the initial values of an object using a set of values (mostly a dictionary).

Convenience init in Swift

Functions in Swift

Above all, Swift functions are written in a slightly different format than that of Objective-C.

// Swift Functions

func myFunc (inp: String) -> (name: String, res: Int)
{
    return ("Hello " + inp, 2)
}

Moreover, there are other ways that you can use functions in swift.

Functions in Swift

Class Extensions in Swift

We use class extension for breaking our code into so many pieces and modularizing it. These extensions in swift are the same as categories in Objective-C. In a real development in swift we depend heavily on class extensions.

Class Extensions in Swift

Class Methods

To enact a function directly from a class we use class methods and not an instanced object of it. Static and Class keywords have similar roles. Both the static and class keywords allow us to attach variables to a class rather than to instances of a class. Where static and class differ is how they support inheritance:

When you make a static property it becomes owned by the class and cannot be changed by subclasses, whereas when you use class it may be overridden if needed.

Class Methods in iOS

Inheritance in Swift

Inheritance is about subclassing, overriding parent class methods and adding custom inits to the subclasses. For example, To override a characteristic that would otherwise be inherited, you prefix your overriding definition with the override keyword. Doing so clarifies that you intend to provide an override and have not provided a matching definition by mistake. Sub-classes can have their own attributes and functions. They can also override base class’s functions

Inheritance in iOS Swift

Classes and Functions in Swift Summary

In conclusion, Classes and Functions in Swift are a fundamental aspect of any good programming exercise and every developer should have a solid grasp over them.

Interested in learning more in-depth concepts?

If you are interested in learning more about mobile app development, I invite you to join my complete iPhone or Android course(s):

Leave a Comment