Skip to main content

15 Best Swift Interview Questions and Answers

What Is Swift Programming?
Swift is a powerful, open-source, safe, multi-paradigm and compiled programming language designed and developed by Chris Lattner, Apple Inc.

It is a safe, fast and interactive programming language for iOS, macOS, watchOS, tvOS and Linux.
Swift took language ideas from Objective-C, C#, Ruby, Python, Rust, Haskell, CLU and so on.
Swift Programming offers better type safety, security and performance.

Some Programming Patterns -
ü  Swift variables are always initialized before use.
ü  Swift array indices are checked for out-of-bounds errors.
ü  Swift is an alternative to the Objective-C language.
ü  Memory is managed automatically.
ü  Error handling allows controlled recovery from unexpected failures.
Swift Version History -
Mid 2010 - Development begins
Jun 2014 - Apple announces Swift at WWDC 2014
Sep 2014 - Apple releases Swift 1.0
Oct 2014 - Swift 1.1 is released
Apr2015 - Swift 1.2 is released
Mar 2017 - Apple releases Swift 3.1
June 2017 - Apple announces Swift 4.0 at WWDC 17

Is Swift Object Oriented Programming?
Yes! Swift is an Object-Oriented Programming language.

What's New Features in Swift 4.0?
ü  Faster, easier to use Strings that retain Unicode correctness
ü  Smart key paths for type-safe, efficient, extensible key value coding for Swift types
ü  Added some enhancements to creating dictionary and Set types
ü  Extends to supports of serialization to Struct
ü  Tuples and multiple return values
ü  Structs that support methods, extensions, and protocols
ü  Native error handling using try/catch/throw

What Are the Advantages of Swift?
ü  Swift is safe
ü  Swift is fast
ü  Swift is open source
ü  Swift is approachable
ü  Swift is easy to learn

What Is The Difference Between Swift And Objective-C Language?
Swift Programming-
In a swift programming, the variables and constants are declared before use.
In a swift programming, “var” keyword used for variable and “let” keyword for constant.
In a swift programming, no need to end code with semi-colon
In a swift programming, does not require creating a separate interface like Objective-C.
In a swift programming, we can define methods in class, structure or enumeration.

Objective-C Programming-
In objective-C programming, we need to end code with semi-colon
In objective-C programming, we can declare constant as int and variable as NSString.

How You define variables in Swift?
The “var” keyword is used for declaring variables and variables must be declared before they are used.
var helloMsg = "This is Anil, How Are You?"


What Is the significance of “?” in swift?
The question mark (?) is used during the declaration of a property. If the property does not hold a value, the question mark (?) helps to avoiding application errors.

Example looks like -
class Employee {
  var certificate : [Certificates]?
 }

let employee = Employee();

Example 2 -
let middleName : String? = nil
let lastName : String = "Singh"
let name : String = middleName ?? lastName


What Is Type Aliasing in Swift?
This borrows very much from C/C++. It allows us to alias a type, which can be useful in many particular contexts.
typealias sample = UInt16


What Is a deinitializer in Swift?
If you want to perform an additional clean-up of your classes, it is possible to define a block called deinit.

Syntax -
deinit {
  //Your cleanup statement here.
}


How Multiple Line Comment Can Be Written In Swift?
The Nested multiline comments enable you to comment out large blocks of code quickly and easily.
Use an opening (/*: ) and closing ( */) comment and it looks like -

/*:
     The above forward slash (/) and an asterisk (*) then a colon (:) is opening comment - (/*:)
    
     The below an asterisk (*) and forward slash is closing comment – (*))
*/

What Is difference between “let” and “var” declaration?
The “var” keyword is used for declaring variables while “let” keyword is used to declare constants.
Both variables and constants must be declared before they are used. 

Once assigned value in the constant, cannot be change but once assigned value in the variables, it can be change.
Var helloMsg = “This is Anil, How Are Yo?” // declared variable
let port = 8080; //declared  constant


How To convert NSMutableArray to Swift Array in swift?
The following code looks like -
var mutableArray = NSMutableArray(array: ["Anil", "Sunil", "Sushil"])
var swiftArray = mutableArray as NSArray as! [String]


How To convert NSArray to NSMutableArray in swift?
The following code looks like -
let array_1: NSArray = [“Anil”, “Sunil”, “Sushil”]
var mutableArray = NSMutableArray(array:array_1)

print(mutableArray)


By Anil Singh | Rating of this article (*****)

Popular posts from this blog

nullinjectorerror no provider for httpclient angular 17

In Angular 17 where the standalone true option is set by default, the app.config.ts file is generated in src/app/ and provideHttpClient(). We can be added to the list of providers in app.config.ts Step 1:   To provide HttpClient in a standalone app we could do this in the app.config.ts file, app.config.ts: import { ApplicationConfig } from '@angular/core'; import { provideRouter } from '@angular/router'; import { routes } from './app.routes'; import { provideClientHydration } from '@angular/platform-browser'; //This (provideHttpClient) will help us to resolve the issue  import {provideHttpClient} from '@angular/common/http'; export const appConfig: ApplicationConfig = {   providers: [ provideRouter(routes),  provideClientHydration(), provideHttpClient ()      ] }; The appConfig const is used in the main.ts file, see the code, main.ts : import { bootstrapApplication } from '@angular/platform-browser'; import { appConfig } from ...

How To convert JSON Object to String?

To convert JSON Object to String - To convert JSON Object to String in JavaScript using “JSON.stringify()”. Example – let myObject =[ 'A' , 'B' , 'C' , 'D' ] JSON . stringify ( myObject ); ü   Stayed Informed –   Object Oriented JavaScript Interview Questions I hope you are enjoying with this post! Please share with you friends!! Thank you!!!

List of Countries, Nationalities and their Code In Excel File

Download JSON file for this List - Click on JSON file    Countries List, Nationalities and Code Excel ID Country Country Code Nationality Person 1 UNITED KINGDOM GB British a Briton 2 ARGENTINA AR Argentinian an Argentinian 3 AUSTRALIA AU Australian an Australian 4 BAHAMAS BS Bahamian a Bahamian 5 BELGIUM BE Belgian a Belgian 6 BRAZIL BR Brazilian a Brazilian 7 CANADA CA Canadian a Canadian 8 CHINA CN Chinese a Chinese 9 COLOMBIA CO Colombian a Colombian 10 CUBA CU Cuban a Cuban 11 DOMINICAN REPUBLIC DO Dominican a Dominican 12 ECUADOR EC Ecuadorean an Ecuadorean 13 EL SALVA...

Encryption and Decryption Data/Password in Angular

You can use crypto.js to encrypt data. We have used 'crypto-js'.   Follow the below steps, Steps 1 –  Install CryptoJS using below NPM commands in your project directory npm install crypto-js --save npm install @types/crypto-js –save After installing both above commands it looks like  – NPM Command  1 ->   npm install crypto-js --save NPM Command  2 ->   npm install @types/crypto-js --save Steps 2  - Add the script path in “ angular.json ” file. "scripts" : [                "../node_modules/crypto-js/crypto-js.js"               ] Steps 3 –  Create a service class “ EncrDecrService ” for  encrypts and decrypts get/set methods . Import “ CryptoJS ” in the service for using  encrypt and decrypt get/set methods . import  {  Injectable  }  from ...

Angular Testing Questions and Answers | 9, 8, 7, 6

What Is Testing? The testing is a tools and techniques for a unit and integration testing Angular applications . Why Test? Tests are the best ways to prevent software bugs and defects. How to Setup Test in Angular Project? Angular CLI install everything you need to test an Angular application. This CLI command takes care of Jasmine and karma configuration for you. Run this CLI command- ng test The test file extension must be “.spec.ts” so that tooling can identify the test file. You can also unit test your app using other testing libraries and test runners. Types of Test – The all great developer knows his/her testing tools use. Understanding your tools for testing is essential before diving into writing tests. The Testing depends on your project requirements and the project cost. The types of Testing looks like - 1.       Unit Test 2.       Integration Test 3.       En...