IQ-Swift PDF PDF
IQ-Swift PDF PDF
IQ-Swift PDF PDF
iOS Proofs D
R E -Devendranath
D N
ii. 30-40 % of the interview goes with vocal and System tasks
DY
E D
R
D N
DY
E D
R
D N
let age = 10
let course = "Swift" DY
E D
R
Swift compiler treats age as Int and course as String.
D N
DY
Here Int and String are datatypes which we specified
explicitly.
E D
R
D N
print(name) // Swift
print(version) // 4.2
let duration = 60
let description = “Course will be completed in
\(duration) days”
D Y
E D
use \(variableName) to embed values in a string.
R
D N
DY
NOTE: Optional variables must be declared as variable using
var keyword.
E D
R
D N
i. Implicit unwrapping DY
There are different ways to unwrap the optional variables.
DY
E D
R
D N
DY
E D
R
D N
D Y
guard let unwrapped = anOptional else {
print("Optional hasDno value")
E
Rbreak
return // exit /
}
D N
print("anOptional has a value. Use it.")
E D
// obValue is accessible with in this block
}
R
N
print(obValue) // obBalue is not accessible here
D
guard let guardValue = anOptional else { return / exit }
DY
print(guardValue) // guardValue is Accessible here
E D
R
D N
enum WeekDay {
case MONDAY, TUESDAY, WEDNESDAY
case THURSDAY
case FRIDAY
}
enum Errors {
case unknownError
case httpError(Int, String)
case networkError(String)
}
struct <#name#> {
<#fields#>
}
struct Structures {
DY
let name = "DNREDDI"
E D
R
let age = 29
let address = "Hyderabad"
func display() {
print(name) D N
print(age)
print(address)
}
}
struct Book {
var name = “Obj-C”
DY
var price = 12.50
E D
R
D N
mutating func modifyableMethod() {
name = "Swift"
price = 22.50
}
}
class Classes {
let name = "DNREDDI"
let age = 29
let address = "Hyderabad"
DY
func display() {
E D
print(name) R
print(age)
D
print(address)
N
}
}
class Person {
Y
let name = "DNREDDI"
D
let age = 29
D
let address = "Hyderabad"
func display() {
print(name)
R E
}
print(age)
print(address)
D N
}
Y
Static properties are accessed over Class or Structure name.
D
E D
Static variables are accessible only in class level or
structure level methods.
R
D N
class Person {
let clothsWeight = 3.50
let actualWeight = 76.50
DY
var totalWeight: Double {
E D
return clothsWeight + actualWeight
}
R
}
D N
Here totalWeight is computed property, which is depend on
clothsWeight and actualWeight.
class Person {
lazy var clothsWeight = 3.50
let actualWeight = 76.50
var totalWeight: Double {
return clothsWeight + actualWeight
}
}
class Person { D Y
E D
static let numberOfLegs = 2
}
R
struct Person { D
N
static let numberOfLegs = 2
}
func aSampleMethod() {
print("Executing block of statements")
}
Functions vs Methods
Functions are global whereas methods are associated to
an Object
Instance methods and type/class methods?
Instance methods are associated with objects
type or class methods are associated with Datatypes
DY
E D
R
D N
print(“Miles: \(miles())) D Y
E D
print(“Miles: \(miles(9.8)))
R
O/P:
D N
Miles: 0
Miles: 7
class Stack {
let itemHolder = [0, 1, 2, 3, 4, 5]
subscript(index: Int) -> Int {
return itemHolder[index]
DY
}
}
E D
R
let aStack = Stack()
D N
aStack.itemHolder[5] // 5
// Using subscript
aStack[5] // 5
class Calc {
return ReturnType
DY
{ (inputArg: Datatype, inpurArg: Datatype) -> ReturnType in
D
}
// Addition Closure
R E
}
return a + b
D N
let sumClosure: (Int, Int) -> Int = {(a: Int, b: Int) -> Int in
DY
E D
R
D N
Syntax:
Y
let temp = a
a = b // Cannot assign to value: 'a' is a 'let' constant
}
b = temp
D
// Cannot assign to value: 'b' is a 'let' constant
D
R E
func swap(a: inout Int, b: inout Int) {
let temp = a
a = b
D N
b = temp
}
var p = 10
var q = 20
swap(a: &p, b: &q)
class Person {
let age: Int
DY
let name: String
E D
init() {
R
age = 29
D N
name = "DNReddi"
}
}
deinit {
print("Extra cleanup of memory")
}
DY
E D
R
D N
E
var b: Int
var c: Int
var d: Int
R
init() {
b = 0
D N
c = 0
d = 0
}
func deferExample() {
defer {
print("This block of code executes just before completing
the execution of this method")
}
DY
prin("1")
prin("2")
E D
prin("3")
R
}
D N
O/P:
1
2
3
This block of code executes just before completing the execution of
this method
DY
E D
R
D N
DY
E D
R
D N
DY
E D
R
D N
DY
E D
R
D N
DY
E D
R
D N
map
filter DY
reduce
E D
flatmap
R
compactMap
D N
anItem * 10
DY
let mappedArray = anArray.map { (anItem) -> Int in
E D
print(mappedArray) // [10, 20, 30, 40, 50]
R
let resultedArray = anArray.map { $0 * 10 }
N
print(resultedArray) // [10, 20, 30, 40, 50]
DY
E D
R
D N
Y
var collectionOfCollections = [[1, 2],[3, 4],[5, 6, 7]]
D
print(flatMapRes)
E D
var flatMapRes = collectionOfCollections.flatMap { $0 }
[1, 2, 3, 4, 5, 6, 7]
R
D N
var collectionOfCollections = [[1, 2],
[3, 4],
[[5, 6, 7], [8, 9]]]
DY
let nonNilCollection = coll.compactMap { $0 }
print(nonNilCollection)
["1", "2", "3"]
E D
R
D N
protocol ArithmaticProtocol {
var aValue: Int {get set}
var bValue: Int {get set} DY
var result: Int { get }
E D
R
func
func
add()
sub() D N
func mul()
func div()
func displayResults()
}
protocol ArithmaticProtocol { D
Y
E D
var aValue: Int {get set}
R
var bValue: Int {get set}
N
var result: Int { get }
D
func add()
func sub()
func mul()
func div()
func displayResults()
}
Reach me for Offline/Online training @iPhoneDev1990@gmail.com
Can we declare optional methods in Protocol?
Yes we can declare optional methods in Protocol. Use @objc
in-front of protocol keyword and @objc optional in-front of
method declaration.
DY
E D
R
D N
Y
// Optional method
D
@objc optional func mul()
}
E D
Write protocol name after the Class name to adopt it.
R
class Calculator: ArithmaticProtocol {
var aValue: Int = 10
func add() {
D N
print("Perform addition here")
}
}
DY
E D
R
D N
DY
E D
R
D N
DY
E D
R
D N
extension String {
func characterAtIndex(index: Int) -> Character {
let stringIndex = String.Index(encodedOffset: index)
}
return self[stringIndex]
DY
E D
func reverseString() -> String {
R
var reversedString: String = ""
} D N
for aCharacter in self.reversed() {
reversedString = reversedString + String(aCharacter)
return reversedString
}
}
DY
E D
R
Functionality added using Functionality added using
D
instances of that class.N
extension is available to all subclassing is only available
to the subclass.
D Y
ARC keeps track of reference counting of the objects. When
E D
reference counting becomes 0 then ARC releases the object.
R
D N
DY
D
Strong
R E
D N Strong
DY
D
Strong
R E
D N Weak
DY
E D
R
D N
DY
E D
R
D N
DY
Array allows to access particular element using index
D
Set doesn’t allow to access particular element as there is no
E
index
R
D N
Array is bit slower when iterating through the collection
Set is faster when iterating through the collection
D Y
Tuples are used to return multiple values from a method
D
Dictionary is to store key-value pairs
E
R
N
Note: Dictionary keys are unique
D
let a = 10
switch a {
case 9:
DY
print("This is 9")
case 10:
E D
print("This is 10")
R
N
fallthrough
case 11:
default: D
print("It is bigger than 10")
O/P:
This is 10
It is bigger than 10
DY
E D
R
D N
DY
E D
R
D N
let a = 10
if a is Int {
Y
print("a is an integer”)
}
D D
R E
D N
DY
E D
R
D N
DY
E D
R
D N
DY
E D
R
D N
func clean() {
DY
}
E D
}
R
D N
Database.dbManager.save()
Database.dbManager.clean()
class Database {
Y
static let dbManager: Database = Database()
D
private init() { }
E D
func save() { }
R
func clean() { D
}
N
}
Database() // Error
Database.dbManager.save()
Database.dbManager.clean()
Reach me for Offline/Online training @iPhoneDev1990@gmail.com
Whats new in Swift 5?
DY
E D
R
D N