- What are the features of Swift programming?
Ans. Following are the features of Swift:
- Type Safety and Type inference language
- Protocol oriented
- Much faster when compared to other languages.
- More impressive structs and enums
- Not required to use semicolons
- Safe by default
- Enforced initializers
- Forced Unwrapping
- Less code, fewer files
- Closures
- Tuples
- Mention the data types in Swift?
Ans. Swift provides a standard set of built-in data types which are used for different purposes:
- Int: It is used to store the integer value
- String: String literals are used to define the text that contains double quotes in Swift.
- Double and Float: They are used in Swift when we have to define decimal value or number.
- Arrays: Arrays are defined for the collection of list items.
- Bool: It is used to store the Boolean value. It uses ‘True’ and ‘False’ conditions
- Dictionaries: A dictionary is used to collect items of a similar type that is connected with a unique key.
- Explain Dictionary in Swift?
Ans. Dictionary is similar to the hash tables in other programming languages. It speeds up the entries available in the table. Each entity uses its key in the table, which can be of any Boolean type such as string or number. These keys can be used to retrieve the concurrent value that can be of any value.
- Mention the tools required to develop iOS applications?
Ans. Following are some tools used to develop iOS applications:
- Xcode: It is an iOS development tool that is used for both iOS apps and MAC OS. The graphic interface in Xcode is used to write code for iOS 8 with Apple’s Swift programming language
- Alcatraz: If you want to add Xcode in your application, you can use Alcatraz. It worked as a package manager for Xcode and used to install multiple plug-ins in your IDE. This tool is only available for Xcode 5+ and OSX 10.9+.
- Marvel: It will convert sketches into collective prototypes. We can add paper sketches to the app, Adobe Creative Cloud, from Dropbox or we, can draw the sketch directly to the app. After adding the sketch, add tappable hot spots in your image to connect screens together. Now, you can export your web clip on a home page.
- Cocoa Controls: It is a list of code used in iOS applications which includes more than 1000 libraries from the open-source community
- What is the meaning of question mark ‘?’ in Swift?
Ans. It is used in the code at the time of the declaration of a property. It has the function to tell the compiler that the available property is optional. By using the question mark ‘?’, we can avoid run time error. It will provide optional chaining and variant with conditional clauses:
var optionalN1 : String? = “John”
if optionalN1 != nil {
print(“Your name is \(optionalN1!)”)
}
- Explain the difference between functions and methods in Swift.
Ans. Functions and methods can be used interchangeably, but there is a small difference between both of them. Methods belong to struts, classes, and enums, but functions do not.
func thisIsAFunction1() {
}
struct Person {
func thisIsAMethod1() {
}
}
- Explain the use of double question mark ‘??’.
Ans. It is used to provide the default value for the variable.
let missingN1 : String? = nil
let realN1 : String = “John Doe”
let existentN1 : String = missingN1 ?? realN1
- What is the procedure to make a property optional in Swift?
Ans. To make a property optional, we have to put a question mark’?’ in it. This ‘?’ symbol will help to avoid run time error if the property does not hold any value.
- Explain how to create constant in Swift programming?
Ans. We can create constant in Swift programming by declaring a ‘let’ keyword. Below is the example where we use declare street as a constant in lieu of a variable. For this, we have to use let keyword:
let street: String = “7th Avenue”
var number: Int
street = “Side Street”
number = 10
We cannot change the value of constant in Swift. If we try to update the first line of the code, the Xcode will show an error for a simple reason. To change the value of the street, we can comment on the given line, so that it will not create any type of error.
let street: String = “7th Avenue”
var number: Int
// street = “Side Street”
number = 10
- What are the common execution states for a Swift iOS App (iOS Application Lifecycle)?
Ans. Following are the common execution states for a Swift iOS Application:
- Not Running: In this state, the application is not launched, or the code is terminated by the system, and it will shut down the whole application.
- Inactive: It is the transactional state of the lifecycle where the application is running in the background, but it is not able to initiate events.
- Active: It is the main execution state in the programming where the application is running in the background and able to initiate the event.
- Background: In this phase, the application is running in the background, and it is still able to execute the code.
- Suspended: This state defines that our application is running at the background state, and the system suspends this application, and the application is not able to execute any code.
- What is Typealias in Swift?
Ans. It allows you to provide a new name to the existing data type in the programming. After declaring the alias name to the existing type, we can use it throughout the programming. It will provide more readability to the code and easy understandability.
typealias AudioS1 = UInt16
- What is the possible way to write multiple line comment Swift?
Ans. To write a multiple line comment, we can use (/*) symbol at the start and (*/) at the end of the line.
- What are the Adapter and the Memento Pattern in Swift?
Ans. Adapter- It converts the interface of a class into other interfaces as required by the client. It covers itself around the objects to reveal a standard interface to connect with that object.
Memento- It is used in iOS as a part of the state restoration. This externalized state of the code can be store without breaching encapsulation. Memento is especially used by Apple for archiving.
- What is the difference between @synthesize and @dynamic in Objective –C?
Ans. @synthesize- It is used to generate a getter and setter method for the property.
@property (nonatomic, retain) NSButton *someButton1;
…
@synthesize someButton1;
@dynamic- It will inform the compiler that getter and setter are implemented at some other place.
@property (nonatomic, retain) IBOutlet NSButton *someButton1;
…
@dynamic someButton1;
- What is the difference between let and var in Swift programming?
Ans. ‘let,’ and ‘var’ are used to declare the functions in JavaScript. The main difference in both of them is that ‘let’ is block-scoped, whereas ‘var’ is the functional scope. As variable is represented by ‘var,’ and it is defined in the whole program in relation to ‘let.’
Example for defining var:
Input:
console.log(y);
var y=7;
console.log(y);
Output: undefined 7
Example for defining let:
Input:
console.log(y);
let x=7; console.l
og(y);
Output: Error
- Explain generics? How to apply a method or variable generics in Swift?
Ans. Generic is used to define reusable and sensitive functions in the code. It is featured in Swift 4. It is used to avoid the risk of duplication in the program. ‘Dictionary’ and ‘Arrays’ are also referred to as a generic collection. We can only add generics to a method but not in a variable.
Example:
func Vector3D(x: [P], y: [P], z: [P])
{ self.xCord = a
self.yCord = b
self.zCord = c
}
Here, the generic is in a function. We change the value ‘T.’ This is the way tocreate generic functions.
- What is an attribute in Swift?
Ans. An attribute is a construct used to provide additional information on the variable declaration and its type. It is represented with @ symbol. There are different arguments with different attributes.
Example:
@available(<attribute_name1>)
@available(<attribute_name1>)(<arguments>)
@available
- How can you add an element into an array?
Ans. An array is a generally used data type allows variable to store and organize data. In Swift, we can use array literals to define our variable to make the task easy. Array elements are distributed by the comma, and the list of values are divided by the square brackets.
Example:
// Add ‘Int’ elements in an Array
let natural number = [1, 2, 3, 4, 5]
// Add ‘String’ elements in an array
let cityName = [“Jaipur”, “Patna”, “Delhi”, “Hydrabad”, “Shimla”]
- Explain the GUARD statement. What is the benefit of using the GUARD statement in Swift?
Ans. It is used to shift the program control out of the scope if there is scope when one or more condition is not met. It is also called as early exit and similar to the ‘if statement’. By using the Guard statement, we can avoid the pyramid of doom.
Example:
guard expression else {
//statements
//must contain a control statement:return, break, continue or throw.
}
- Explain the different ways to unwrap an optional in Swift.
Ans. Below are the seven ways to unwrap an optional in Swift:
- Guard statement:
- Optional chaining:
- Forced unwrapping: using “!” operator, unsafe.
- Optional binding:
- Nil coalescing operator:
- Optional pattern:
- Implicitly unwrapped variable declaration: unsafe in many cases.