Popular Advanced iOS Interview Questions Flashcards
Explain Objective-C in OS.
Since the 1990s, Objective-C has been used by Apple as an object-oriented programming language. This language combines the advantages of two earlier languages - C and Smalltalk. As a superset of C, it provides object-oriented functionality and a dynamic runtime environment.
Features of Objective-C :
- In Objective C, meta classes are automatically created and managed during runtime.
- Dynamic typing and static typing are both supported.
- It is easy to understand.
Name the most important data types in Objective-C?
Following are data types that the developers mostly use in Objective – C:
- BOOL: Represents a Boolean value that is either true or false. Both the _Bool or BOOL keywords is valid.
Example:
_Bool flag = 0;
BOOL secondflag = 1;
- NSInteger: Represents an Integer.
Example:
typedef long NSInteger;
typedef int NSInteger;
- NSUInteger: Represents an unsigned integer
Example:
typedef unsigned long NSUInteger;
typedef unsigned int NSUInteger;
- NSString: Represents a string.
Example:
NSString *greeting = @”Hello”;
Explain Swift in iOS?
Swift is the fastest-growing programming language today, created by Apple. With a significant advantage pool over the well-known Objective-C, Swift holds a leading position in iOS development. It’s an entirely new language created specifically to develop software for Apple’s operating systems. Since Swift implements all the features of other modern languages, you can find type inference, optional, generics, and such higher-order functions. It is compatible with macOS, iOS, watchOS, and tvOS.
What are the important features of Swift?
Swift programming language is being designed so that developers can write correct programs and maintain them easily. It offers the following features:
- Safety: Swift is an efficient way to write programs. Checking code before it is used in production is very important. Apple Swift removes any unsafe code before it is used in production.
- Simple syntax: Swift’s syntax is simple and easy-to-use, just as developers would expect it. The syntax features of Swift enable you to write more expressive code.
- Readability: Swift has a simple syntax, which is easier to read and write. It is easier for developers to write Swift code since it is more similar to plain English, enabling them to spend less time looking for problematic code.
- Multiplatform support: Swift is fully compatible with iOS, macOS, tvOS, watchOS, Linux, and many other platforms. This means you are able to develop software that is compatible with all operating systems.
- Open-source: Swift is developed at swift.org, an open-source framework. For Swift to become a defining programming language, the technology had to be open to all. Swift supports all Apple platforms and makes programming easier, faster, and safer.
- Compatible with Objective C: Swift has full compatibility with Objective-C. Swift enables programmers to import frameworks from Objective-C using the Swift syntax. Programmers can utilize Objective-C libraries and classes inside Swift code.
Explain NSError in Swift.
Information about an error condition is encapsulated within an NSError object in an extendable and object-oriented manner. An NSError object is comprised of three basic attributes: a predefined error domain (represented as a string), a domain-specific error code, and a user info dictionary containing application-specific information.
Example: The examples below show how to create custom errors.
NSString *domain = @”com.MyCompany.MyApplication.ErrorDomain”;
NSString *desc = NSLocalizedString(@”Unable to complete the process”, @””);
NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : desc };
NSError *error = [NSError errorWithDomain:domain code:-101 userInfo:userInfo];
What is Enum or Enumerations in Swift?
The term enumeration refers to a user-defined data type consisting of a set of related values that allows you to work with those values in your code in a type-safe manner. An enumerated data type is defined by the keyword enum.
Syntax:
enum enum_name
{
// enumeration values are described here
}
Example:
enum MonthsofaYear
{
case January
case Februrary
…
case December
}
Values defined in an enumeration MonthsofaYear such as January, February, and so on until December are its enumeration cases. New enumeration cases can be introduced using the case keyword. You can put multiple cases on a single line, separated by commas as follows:
enum MonthsofaYear
{
case January, February,….,December
}
What do you mean by lazy property in iOS?
Lazy properties are properties whose initial value isn’t computed until the first time they are used. Including the lazy modifier/keyword before the declaration of a stored property indicates it is lazy.This lets you delay the initialization of stored properties.
This could be a great way to streamline your code and reduce unnecessary work. When a code is expensive and unlikely to be called consistently, a lazy variable can be a great solution.
Example:
class Person {
var name: String
lazy var personalizdgreeting : String = {
return “HelloScala (self.name)!”
}()
init(name: String) {
self.name = name
}
}
As shown above, we aren’t sure what value personalizedgreeting should have. To know the correct greeting for this person, we need to wait until this object is initialized.
What are generics in swift and write its usage?
A major feature of Swift is generics, and much of the Swift standard library is written in generic code. Swift’s ‘Array’ and ‘Dictionary’ types, for example, constitute generic collections. Generic code allows you to create flexible, reusable functions and types that work with any data type. You can create code that does not get too specific about underlying data types, resulting in cleaner code.
Example:
func Swapping(x: inout Int, y: inout Int)
{
let temp = x
x = y
y = temp
}
var num1 = 10
var num2 = 50
print(“Before Swapping: (num1) and (num2)”)
Swapping(x: &num1, y: &num2)
print(“After Swapping: (num1) and (num2)”)
Output:
Before Swapping: 10 and 50
After Swapping: 50 and 10
In the above example, we have defined a function called Swapping() to swap integers. It takes two parameters x and y of int type. As seen in the output, the values of x and y are exchanged after the swapping.
Explain dictionary in Swift.
Swift programming defines a dictionary as an unordered collection of items. It stores items in key-value pairs. A dictionary uses a unique identifier called a key to store an associated value which can later be referenced and retrieved through the same key.
Syntax:
var Dict_name = KeyType: ValueType
Example:
var examresult= [“Rahul”: “79”, “Aditya”: “86”, “Aditi”: “67”]
print(examresult)
Output:
[“Rahul”: “79”, “Aditya”: “86”, “Aditi”: “67”]
In the above example, we have created a dictionary named examresult. Here,
Keys are “Rahul”, “Aditya”, “Aditi”
Values are “79”, “86”, “67”
Why design patterns are important? Name some of the popular design patterns used in iOS?
A design pattern is a solution to a specific problem you might encounter while designing an app’s architecture. They are templates designed to make coding easier and more reusable for you. Following are some of the design patterns that can be used in iOS:
- MVC (Model-View-Controller):
MVC is a design pattern for developing web applications in which three components are connected i.e., view, controller, and model. Views can be conceived as the user interface shown to the end-user at a certain point in time. The model represents the data to be shown on the view. The controller acts as a link between view and model. There is always a relationship between these three components.
- MVVM (Model-View-View Model):
Unlike MVM, there is a special layer in MVVM called the View Model between the View and the Model. Using a view model, model information can be transformed into values that can be displayed on the view. There is a binding link between the view and View model that allows the view model to share updates with the view.
- Facade:
The facade design pattern provides a simplified (but limited) interface to a complex set of classes, libraries or frameworks. As opposed to providing the user with a set of classes and their APIs, you instead provide a simple, unified API. While Facade helps to reduce overall application complexity, it also helps to move unwanted dependencies to a single location.
What is the JSON framework supported by iOS?
iOS supports the SBJson framework as the JSON framework. Humans and computers alike can easily read and write this lightweight data exchange formatter. JSON handling is simplified with SBJson’s flexible APIs and additional control.
Explain iBeacons in iOS.
iBeacon, Apple’s new low-energy Bluetooth wireless technology, allows iPhone and other iOS users to receive location-based information and services on smartphones. IBeacons are small, wireless transmitters that transmit signals to nearby smart devices via Bluetooth low energy technology.
State the difference between KVC and KVO in Swift.
KVC (Key-Value Coding): It enables object properties to be accessed at runtime using strings rather than knowing the property names statically during development.
KVO (Key-Value Observing): In Objective-C and Swift, KVO is one of the methods for observing program state changes. If an object has instance variables, KVO enables other objects to observe the changes to those variables.
Explain TDD (Test-Driven Development).
Software developers can use testing-driven development (TDD) in the development of software.
In TDD, developers plan the features of the software that they want to create and then write test cases for each feature before implementing it.
Through test-driven development, we can gain insight into both the quality of the implementation (does it work) and the quality of the design (is it well structured).
1.FAIL –>2.PASS—>3. REFACTOR
At first, the test case will fail because the code is not yet implemented, and this is usually referred to as the red phase. After that, code is written to ensure that the test case passes and does not break any components or current test cases, which is called the green phase. The developer should then refactor the implementation of the code by cleaning and maintaining the codebase, as well as optimizing the efficiency. This process should then be repeated every time a new test case is added.
Explain the function of the completion handler.
Completion handlers are basically just functions passed as parameters to other functions. They are used to dealing with the response of asynchronous tasks since we do not know when they will end. Completion handlers inform an application when an operation, such as an API call, has been completed. The program is informed that the next step needs to be executed.
Example:
Let’s create a simple class called CompletionHandler that has one method called count that counts from 0 to 50. Once it reaches 25 (a random value), it will make a network request to https://scaler.com. Once the request has been completed, we will print Received response.
class CompletionHandler {
func count() {
for i in 0…50 {
if i == 25 {
if let url = URL(string: “https://scaler.com”) {
URLSession.shared.dataTask(with: url) { (data, response, error) in
print(“Received response”)
}.resume()
}
}
print(“I = “, i)
}
}
}
let newInstance = CompletionHandler()
newInstance.count()
You will see all the numbers printed out in the console as soon as the code runs, but the Received response will be printed only after all those other numbers have been printed.