Ans : In Swift, there are multiple ways to write utility functions.
Static Functions
Static functions are invoked by the class itself, not by an instance. This makes it simple to invoke utility functions without having to manage an object to do that work for you.
classAppUtils{staticfuncappUtility(){}}
We can access static function as AppUtils.appUtility()
Static functions can not be overridden.
Class Functions
Class functions (not instance methods) are also static functions but they are dynamically dispatched and can be overridden by subclasses unlike static functions.
We can access them similar to static functions as AppUtils.appUtility() and AppOtherUtils.appUtility().
static is same as class final.
Global Functions
But then you can also just do a stand alone function in Swift which is not within a class and access it anywhere in the project. The global functions can be kept in a separate file that we can import into any project as per requirement.
funcappUtility(){}
We can just access them as appUtility() anywhere in the project. If you have method with same name as of global function, then access global function with MyAppName.appUtility()
In case of static functions, if we access one of the static member, entire class gets loaded in memory. But in case of global function, only that particular function will be loaded in memory.
So, which one is better to use?
It is largly subjective why we pick one over another. Some prefer static method approach as a form of namespacing. For example, using the static method approach also allows us to have a method ClassA.appUtility() and a method named ClassB.appUtility(), which is useful while developing a library or framework.
Global functions are more modular and factor out single tasks for a single function - a good global utility function that does exactly one thing and does it perfectly can also sometimes be abstracted or made generic and used in other context as well.
We can just create a AppUtility.swift file and put all the utility functions in it. Later this file can be used across multiple projects.
Encapsulation hides variables or some implementation that may be changed so often in a class to prevent outsiders access it directly. They must access it via getter and setter methods.
Abstraction is used to hiding something too but in a higher degree(class, interface). Clients use an abstract class(or interface) do not care about who or which it was, they just need to know what it can do.
Encapsulation: Wrapping code and data together into a single unit. Class is an example of encapsulation, because it wraps the method and property.
Abstraction: Hiding internal details and showing functionality only. Abstraction focus on what the object does instead of how it does. It provides generalized view of classes.
int number = 5;
string aStringNumber = number.ToString();
Here, ToString() is abstraction. And how this mechanism number variable converted to string and initialize into aStringNumber is encapsulation.
We can achieve abstraction using protocol in iOS, achieve encapsulation using access control in class or struct.
An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.
Enumerations offer an easy way to work with sets of related constants. An enumeration, or Enum, is a symbolic name for a set of values. Enumerations are treated as data types, and you can use them to create sets of constants for use with variables and properties.
When to use an enum?
Whenever a procedure accepts a limited set of variables, consider using an enumeration. Enumerations make for clearer and more readable code, particularly when meaningful names are used.
The benefits of using enumerations include:
Reduces errors caused by transposing or mistyping numbers.
Makes it easy to change values in the future.
Makes code easier to read, which means it is less likely that errors will creep into it.
Ensures forward compatibility. With enumerations, your code is less likely to fail if in the future someone changes the values corresponding to the member names.