All Categories

Quick Interview Points & Best Practices in Swift

This blog has two sections. The first section deals with some of the key interview related points and the second section explains the best practice while dealing with strings used as asset identifier or segue identifier.

Quick tips and key interview points in swift

  • In swift if you take any type it is a first class citizen.
  • typealias keyword allow to use a alias name for an existing type. (Similar to typedef in C language). “typealias” best used to re – using block declaration or naming a tuple type in swift
  • semicolon is not mandatory at the end of each statement in swift
  • A Self requirement in protocol act as a place holder and it refers to the type that will adopt the protocol
  • A protocol that contains Self(Self requirement) somewhere inside can only be used associated with a type that has adopted the protocol.
  • The only difference between the usage of Static and class keyword usage in swift is that, we can override method marked with class keyword in a subclass where as it is not possible to override the method marked with static/final keyword in subclass.
  • Static field declared are effectively lazy by default
  • Anonymous closure can be used to make a member variable behave lazy as well as assign a runtime value to the member variable.The following code show the example for an anonymous closure

var someIntegerValue = { (x:Int) -> Int in
//Can implement any logic here
return (x * x);

  • @UIApplicationMain is the preprocessor directive that is declared above the app delegate in swift. Traditional main method seen iOS Objective C projects is not present anymore(By default main method is not present. However we can add main method) and is replaced with this directive. @UIApplicationMain is the default way that which is found in the iOS Swift project. @UIApplicationMain directive marks the entry point of the application.
  • Struct in swift does not have a deinit method
  • Mutating method need to be present to change the value of structs
    The bridging header is mentioned in the Swift Compiler – Code Generation section of build settings tab.

The property Objective-C Generated Interface Header Name defines the header name that need to be imported in Objective C file while trying to use swift within Objective C.

The header file mentioned for the property Objective-C Bridging header is where the name of the header that will be exposed to the swift is imported.

  • AnyObject is a protocol and can represent an instance of class type. AnyObject can be used in place where id was used in Objective C.
  • The value represented by swift enum could be a string, a character, or a value of any integer or floating-point type.
  • Swift support implementation to define custom subscript for Classes, structures, and enumerations. Subscript can be defined as short hand syntax for querying a particular type by writing values inside the square brackets ie., []. For example subscript is used for accessing an element in an array.

Best practice while dealing with asset identifier or segue identifier

Let look into the current scenario where we use the asset identifier or segue identifier.

They are generally used while trying to initialize an image object or to perform segue action. The identifiers are strings. As far as compiler is concerned there is no difference between a normal string and a string identifier representing a valid image asset or a segue
Thus it is not possible at compile time to validate if the string is representing a valid image asset or a segue. While using an asset or a segue identifier compiler cannot ensure that the correct name/string is been used. The auto completion feature will not have these string listed. Thus it becomes the responsibility of a developer to ensure the correct name of the string identifiers is been used or else runtime error would be caused. Moreover these identifiers are used multiple time therefor the total burden caused added up to the total number of times the identifier is used. In the situation when an asset/segue is renamed/changed/deleted it falls upon the developer to make the corresponding changes in those areas where these string identifiers have been used. Such dependency reduces the compile time safety paradigm

Hope some of you might have come across similar situation. After reading/analyzing the above situation one might definitely come to support a best practice that would prevent such an unsafe programming practice.

The root cause can be attributed to the usage of the string identifier for asset/segue. This should sound weird, as the identifiers are those, which the iOS developer framework support and thus it is not possible to avoid the usage of string identifier for the asset/segue.

The best we could do is to minimize the usage of string identifiers. One approach would be by using a global constant, however this would clog the global namespace. Thus using static member variables for struct to represent each string identifier would be a solution. The following is a sample code that shows the usage of struct and static member variable

struct Constants {
struct ImageName{
static let ImageNameOne = "ImageNameOne"
static let ImageNameTwo = "ImageNameTwo"
struct SegueName{
static let SegueNameOne = "SegueNameOne"
static let SegueNameTwo = "SegueNameTwo"
var image = UIImage(named:Constants.ImageName.ImageNameOne)

This solves the problem associated with repetitive usage of string and the auto completion feature will be supported thus making the developer job easy.

The usage of constants to represent the string identifier solved most of our problem. However there is no way for the compiler mark a clear distinction between an asset identifier or a segue identifier. In the case when the asset identifier is used in place for segue or vice versa the compiler won’t complain.

An elegant solution or best practice that Apple has suggested to handle such situation is by using enumeration to define the asset /segue identifier and write extension methods to create function that accept the enum value and in turn call the corresponding method. Now let see it in action and have a look into some code. The following code depicts the usage of enum for UIImage initialization instead of string.

Create the enum representing the asset identifier

enum ImageAsset:String {
case ImageOne = "ImageOne",ImageTwo = "ImageTwo"

Create the extention for UIImage class that adds initializer that accepts ImageAsset enum and initializes the UIImage instance. Since the valid image name will be used the failable initializer for the UIImage is force unwrapped.

extension UIImage{
convenience init(asset:ImageAsset){
self.init(named: asset.rawValue)!

Initializing UIImage using enumeration

var sampleImage = UIImage(asset:.ImageOne)

In a similar manner a separate enum to represent the segue could be created and used appropriately and thus a clear distinction could be marked with assets and the segue. Thus the developer need to be careful only while creating the case for enum.

Hope you enjoyed reading ! Become a Certified iOS Developer in 12 Weeks. For more info visit here

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles