121.What is Property?

Swift 4 language provides properties for class, enumeration or structure to associate values. Properties can be further classified into Stored properties and Computed properties.

Difference between Stored Properties and Computed Properties

Stored Property

Computed Property

Store constant and variable values as instance

Calculate a value rather than storing the value

Provided by classes and structures

Provided by classes, enumerations and structures

Both Stored and Computed properties are associated with instances type. When the properties are associated with its type values then it is defined as ‘Type Properties’. Stored and computed properties are usually associated with instances of a particular type. However, properties can also be associated with the type itself. Such properties are known as type properties. Property observers are also used

To observe the value of the stored properties

Stored Properties:

Swift 4 introduces Stored Property concept to store the instances of constants and variables. Stored properties of constants are defined by the ‘let’ keyword and Stored properties of variables are defined by the ‘var’ keyword.

During definition Stored property provides ‘default value’

During Initialisation the user can initialise and modify the initial values

Note: Unlike stored properties, Swift requires you to use an explicit type with your computed properties.

Lazy Stored Property:

Swift 4 provides a flexible property called ‘Lazy Stored Property’ where it won’t calculate the initial values when the variable is initialised for the first time. ‘lazy’ modifier is used before the variable declaration to have it as a lazy stored property.Lazy Properties are used to delay object creation.

When the property is dependent on other parts of a class, that are not known yet

Computed Properties.Rather then storing the values computed properties provide a getter and an optional setter to retrieve and set other properties and values indirectly.Lazy stored properties are used for a property whose initial values is not calculated until the first time it is used. You can declare a lazy stored property by writing the lazy modifier before its declaration. Lazy properties are useful when the initial value for a property is reliant on outside factors whose values are unknown.

Bonus Tip : You do need to declare your lazy property using the var keyword, not the let keyword, because constants must always have a value before initialization completes.

Benefit of lazy property increase performance in terms of speed.

Computed Properties as Property Observers:

In Swift 4 to observe and respond to property values Property Observers are used. Each and every time when property values are set property observers are called. Except lazy stored properties we can add property observers to ‘inherited’ property by method ‘overriding’.

Property Observers can be defined by either

Before Storing the value — will set

After Storing the new value — did set

When a property is set in an initialiser will set and did-set observers cannot be called.

In addition to stored properties, classes, structures, and enumerations can define computed properties, which do not actually store a value. Instead, they provide a getter and an optional setter to retrieve and set other properties and values indirectly.

122. What are the most important application delegate methods a developer should handle ?

The operating system calls specific methods within the application delegate to facilitate transitioning to and from various states. The seven most important application delegate methods a developer should handle are:

application:willFinishLaunchingWithOptions:

Method called when the launch process is initiated. This is the first opportunity to execute any code within the app.

application:didFinishLaunchingWithOptions:

Method called when the launch process is nearly complete. Since this method is called is before any of the app’s windows are displayed, it is the last opportunity to prepare the interface and make any final adjustments.

applicationDidBecomeActive:

Once the application has become active, the application delegate will receive a callback notification message via the method applicationDidBecomeActive.

This method is also called each time the app returns to an active state from a previous switch to inactive from a resulting phone call or SMS.

applicationWillResignActive:

There are several conditions that will spawn the applicationWillResignActive method. Each time a temporary event, such as a phone call, happens this method gets called. It is also important to note that “quitting” an iOS app does not terminate the processes, but rather moves the app to the background.

applicationDidEnterBackground:

This method is called when an iOS app is running, but no longer in the foreground. In other words, the user interface is not currently being displayed. According to Apple’s UIApplicationDelegate Protocol Reference, the app has approximately five seconds to perform tasks and return. If the method does not return within five seconds, the application is terminated.

applicationWillEnterForeground:

This method is called as an app is preparing to move from the background to the foreground. The app, however, is not moved into an active state without the applicationDidBecomeActive method being called. This method gives a developer the opportunity to re-establish the settings of the previous running state before the app becomes active.

applicationWillTerminate:

This method notifies your application delegate when a termination event has been triggered. Hitting the home button no longer quits the application. Force quitting the iOS app, or shutting down the device triggers the applicationWillTerminate method. This is the opportunity to save the application configuration, settings, and user preferences.

123. How do you debug and profile code on iOS?

No one writes perfect code, and developers need to debug their code and profile apps for performance and memory leaks.

There’s always NSLogging and printing in iOS apps. There are breakpoints you can set using Xcode. For performance of individual pieces of code, you could use XCTest’s measureBlock.You can do more advanced debugging and profiling using Instruments. Instruments is a profiling tool that helps you profile your app and find memory leaks and performance issues at runtime.Instrument is a powerful performance tuning tool to analyse that performance, memory footprint, smooth animation, energy usage, leaks and file/network activity.

124. What is three triggers for a local notification and use of UserNotifications?

Location, Calendar, and Time Interval. A Location notification fires when the GPS on your phone is at a location or geographic region. Calendar trigger is based on calendar data broken into date components. Time Interval is a count of seconds until the timer goes off.

We can add audio, video and images.

We can create custom interfaces for notifications.

We can manage notifications with interfaces in the notification centre.

New Notification extensions allow us to manage remote notification payloads before they’re delivered.

125. nil / Nil / NULL / NSNull

NULL: literal null value for C pointers

nil: literal null value for Objective-C objects

Nil: literal null value for Objective-C objects

NSNull: singleton object used to represent null

126. What is the difference between functions and methods in Swift?

Functions are self-contained chunks of code that perform a specific task. You give a function a name that identifies what it does, and this name is used to “call” the function to perform its task when needed. With functions, there is no self.

Methods are functions that are associated with a particular type. Classes, structures, and enumerations can all define instance methods, which encapsulate specific tasks and functionality for working with an instance of a given type. The method can access self.

func someFunc{

//some code

}

class someClass{

func someMethod{

//some code

}

}

127.What are optional binding and optional chaining in Swift?

Optional bindings or chaining come in handy with properties that have been declared as optional. Consider this example:

class Student {

var courses : [Course]?

}

let student = Student()

Optional chaining:

If you were to access the courses property through an exclamation mark (!) , you would end up with a runtime error because it has not been initialised yet. Optional chaining lets you safely unwrap this value by placing a question mark (?), instead, after the property, and is a way of querying properties and methods on an optional that might contain nil. This can be regarded as an alternative to forced unwrapping.

Optional binding:

Optional binding is a term used when you assign temporary variables from optionals in the first clause of an if or while block. Consider the code block below when the property courses have yet not been initialised. Instead of returning a runtime error, the block will gracefully continue execution.

if let courses = student.courses {

print(“Yep, courses we have”)

}

The code above will continue since we have not initialised the courses array yet. Simply adding:

init() { courses = [Course]() }

Will then print out “Yep, courses we have.”

128. What’s the syntax for external parameters?

The external parameter precedes the local parameter name.

func yourFunction(externalParameterName localParameterName :Type, ….) { …. }

A concrete example of this would be:

func sendMessage(from name1 :String, to name2 :String) { print(“Sending message from \(name1) to \(name2)”) }

129.Would you consider yourself a Swift expert?

This is a trick question. Because Swift is still being improved, the only true “Swift Experts” are the people who developed it at Apple. Even if you’ve produced lots of great apps and mastered a number of advanced courses and tutorials, you simply can’t be an expert at this point. Remember, Swift has only been around from sometime. If your interviewer asks this, argue that you could not possibly be an expert until the language is finished. Explain how you, like most other iOS Developers, are constantly learning.

130.What are blocks and how are they used?

Blocks are a way of defining a single task or unit of behaviour without having to write an entire Objective-C class. Under the covers Blocks are still Objective C objects. They are a language level feature that allow programming techniques like lambdas and closures to be supported in Objective-C. Creating a block is done using the ^ { } syntax:

myBlock = ^{

NSLog(@”This is a block”);

}

It can be invoked like so:

myBlock();

It is essentially a function pointer which also has a signature that can be used to enforce type safety at compile and runtime. For example you can pass a block with a specific signature to a method like so:

- (void)callMyBlock:(void (^)(void))callbackBlock;

If you wanted the block to be given some data you can change the signature to include them:

- (void)callMyBlock:(void (^)(double, double))block {

block(3.0, 2.0);

}

--

--

Sandeep Reddy Challa

I have around 14 years of experience in iOS development and lead/senior developer