Вы находитесь на странице: 1из 97

DY

iOS Proofs D
R E -Devendranath

D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


iOS Interview Process
i. 60% - 70 % of the interviews goes vocal

ii. 30-40 % of the interview goes with vocal and System tasks

Out of 30% - 40%


90-95% of the people follow i then ii.
5% - 10% of the people follow ii then i.

You should be able to explain the concepts vocally.

What am I going to do?


i. Preparing a play list for all Interview questions
ii. Prepare a play list for mostly asked systems tasks.
Swift Features or Swift vs Obj-C?
i.Simple to read and learn
ii. Less code
iii. Swift is type safe language
iv. Auto import of files
v. No pointers to confuse
vi. Better Memory Management
DY
vii. Optionals make applications crash free and Bug Free
iix. Faster and Safer
E D
R
ix. Swift is Modern and Protocol Oriented Programming
Language
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


Whats new in Swift 4?
Multiline String
Codable
Strings are collection types
One sided Range Operators

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


Whats new in Swift 4.2?
i. Simple to Generate Random number using random().

let aRandom = Int.random(1…1000)

ii. CaseIterable Protocol for enums. It helps to get allCases


values.
DY
iii. Dynamic Member Lookup
E D
R
iv. Boolean Toggling (toggle) alters the value

vi. Easy to remove array elementsD N


v. New higher order functions (allSatisfy)

vii. #warning and #error diagnostics


iix. Conditional Conformance of Protocols
extension Array: Purchaseable where Element: Purchaseable { … }

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What are the Datatypes available in Swift?
Int
Float
Double
Character
String DY
Bool
E D
Array R
Dictionary
D N
Set
Any

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is let & var?
let is a keyword which is used to declare CONSTANT variable
whose content can’t be changed

var is a keyword which is used to declare variable whose


content is can be changed.

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is Type Inference?
Type Inference is a feature of swift compiler, where it
identifies the datatype of variable based on the value
provided

let age = 10
let course = "Swift" DY
E D
R
Swift compiler treats age as Int and course as String.

D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is Type Annotation?
Type Annotation is a process of explicitly specifying the type
of the variable.

let age: Int = 10


let course: String = "Swift"

DY
Here Int and String are datatypes which we specified
explicitly.
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is Tuple?
Tuple is a process of grouping multiple values together as a
single variable

let bookInfo = ("Swift", 4.2)


print(bookInfo.0) // Swift
print(bookInfo.1) // 4.2
D Y
D
print(bookInfo.name) //E
let bookInfo = (name: "Swift", version: 4.2)
R // 4.2
Swift
N
print(bookInfo.version)
D
let (name, version) = ("Swift", 4.2)

print(name) // Swift
print(version) // 4.2

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is String Interpolation?
Process of embedding values into a string.

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

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is Optional?
Optional variables are capable of holding values and
absence of value(nil).

Use ? symbol after Type Annotation to declare an


Optional
DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


How the optional is declared?
var anOptional: Int? = nil
anOptional = 10

Here anOptional is holding nil or 10.

DY
NOTE: Optional variables must be declared as variable using
var keyword.
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is Optional unwrapping?
Process of extracting underlying value of an optional
variable is known as Optional unwrapping.

Unboxing optional variable’s value.

i. Implicit unwrapping DY
There are different ways to unwrap the optional variables.

ii. Force unwrapping


E D
R
iii. Explicit unwrapping (Optional Binding & guard
statement)
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is Implicit Optional unwrapping?
Process of conveying to the compiler that the optional
variable will definitely contains a value before it is used.

var impOptional: Int!

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is optional binging?
Optional binding is one of the optional unwrapping
techniques.

var anOptional: Int?


anOptional = 19
if let unwrappedOptional = anOptional {
print("Unwrapped value is: \(unwrappedOptional)")
}
What is Optional Chaining?
Optional chaining is process of accessing Optional’s sub
properties which are again optionals.

let res = anOptional?.subOptional?.subOptional


let res = anOptional?.subOptional?.subOptional?.method()

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is nil coalescing operator (??)?
Process of providing default value is optional doesn’t
contain a value.

Process of substituting a value if optional doesn’t contain a


value.

let anOptional: Int?


anOptional = 10
let unwrapped = anOptional ?? 0
What is guard?
guard is one of the optional unwrapping techniques.

var anOptional: Int?


anOptional = 10

D Y
guard let unwrapped = anOptional else {
print("Optional hasDno value")
E
Rbreak
return // exit /
}
D N
print("anOptional has a value. Use it.")

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is Optional Binding?
Optional binding is one of the optional unwrapping
techniques.

let anOptional: Int?


anOptional = 10
DY
E D
if let unwrapped = anOptional {
R
print("anOptional has a value. Use it
N
with in this block.")
D
}

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


Optional Binding vs Guard
The unwrapped value in optional binding is accessible with that
block.

The unwrapped value in guard statement is accessible to the


below statements.

var anOptional: Int? = 10


DY
if let obValue = anOptional {

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 }

print(guardValue) // guardValue is Accessible here

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


Can we access the guard statement value in else block?

guard let guardValue = anOptional else {


// guardValue is not accessible
return
}

DY
print(guardValue) // guardValue is Accessible here

E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What are the new operators available in swift?

Closed Range Operators (a…b)


Half Open Range operators (a..<b)
One sided open range operators (…n, n…)
Nil coalescing operator (??) DY
Type Checking operator (is)
E D
R
Type Considering Operator (as)
D N
Identify Operators (=== / !==)

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is enum & Syntax?
Enum is a keyword, which is used to define user defined
datatype with user specified values.

enum WeekDay {
case MONDAY, TUESDAY, WEDNESDAY
case THURSDAY
case FRIDAY
}

Enum with Raw Values:

enum WeekDay: Int {


case MONDAY = 0, TUESDAY = 2, WEDNESDAY = 3
case THURSDAY = 4
case FRIDAY = 5
}
Enum with Associated Values:
enum Errors {
case unknownError
case httpError(Int, String)
case networkError(String)
}
What is Associated value in enum?
A enum case which is capable of holding additional
information.

enum Errors {
case unknownError
case httpError(Int, String)
case networkError(String)
}

Here, httpError case holds Int and String


networkError holds String
What is Struct & Syntax?
struct is a keyword to create user defined datatype. structure
variables are value types

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)
}
}

Note: Swift structs does contain methods also.

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is mutating method?
The structure variables can’t be modified in structure
methods. To modify properties of a structure we use
mutating keyword as below.

struct Book {
var name = “Obj-C”
DY
var price = 12.50
E D
R
D N
mutating func modifyableMethod() {
name = "Swift"
price = 22.50
}
}

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is typealiasing?
Type aliasing is the process of renaming existing datatype to
a simple name. Int ==> BigInt, String ==> Text

typealias BigInt = Int


let a: BigInt = 123134234

typealias Text = String


let message: Text = "This is text, holded by Text datatype"
What is class and Syntax?
class is a keyword which is used to create a custom / user
defined datatype. Classes are reference types.

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
}
}

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is Class vs Struct?
Structures Classes

structures are value types Classes are reference types

Structures doesn’t allow


Classes does allow inheritance
inheritance

Structure variable values are Class variable reference / address


copied when passed them as is copied when passing them as
arguments to a method arguments to a method

deinitilizers are not available for deinitilizers are available in


structs classes
What is Property?
Property is a variable in an Object which holds values of that
object. Those can be modified to change the appearance of
that object.

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
}

Here, name, age and address are the properties of a Person


class.

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What are the different types of properties available?
Stored Properties
Computed Properties
Lazy Properties
Static Properties / Class Properties
What is Stored Property?
Stored properties directly hold values.
class Classes {
static let name = "DNREDDI"
let age = 29
let address = "Hyderabad"
}
What is type or static properties?
Static properties are class level or structure level
properties.

Static properties are associated with the class or structure.

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

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is Computed Property?
Computed properties are calculated when we try to access
them. The computed properties depends on other properties
of a class or structure.

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.

Note: Computed properties must be var.

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is Lazy Property?
A property whose memory is allocated when it is
accessed or used.

class Person {
lazy var clothsWeight = 3.50
let actualWeight = 76.50
var totalWeight: Double {
return clothsWeight + actualWeight
}
}

Note: Lazy property should always be var


What is Type Property?
A Property which is associated with a struct or class /
Datatype.

Type properties can be declared by using static keyword

class Person { D Y
E D
static let numberOfLegs = 2
}
R
struct Person { D
N
static let numberOfLegs = 2
}

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is function? Syntax?
Function is a block of statements, which performs some
specific task.

func <#name#>(<#parameters#>) -> <#return type#> {


<#function body#>
}

It contains a name, parameters, return type and


body(Block of statements)

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

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


How to set default values for input params?

func miles(kilometers: Double = 0) -> Double {


return kilometers / 1.4
}

print(“Miles: \(miles())) D Y
E D
print(“Miles: \(miles(9.8)))

R
O/P:
D N
Miles: 0
Miles: 7

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


How to pass variable number of arguments to a func?
func numbers(numbers: Int...) {
for i in numbers {
print(i)
}
}
DY
numbers(numbers: E
1)
D
R
numbers(numbers:
numbers(numbers: D N 3,5)
1,2,5)
numbers(numbers: 1,2,3,4,5)

Note: Here, the input argument name numbers become


an Array of Int.
Reach me for Offline/Online training @iPhoneDev1990@gmail.com
What is Subscript? Syntax?
Subscript are a shortcut way to access struct / Enum / Class
’s collections, sequence or lists.

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

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is Generics function?
A method which is capable of taking different datatypes
and perform operations called generic method.

class Calc {

func add(a: Int, b: Int) -> Int {


return a + b
}

func add(a: Float, b: Float) -> Float {


return a + b
}

func add(a: Double, b: Double) -> Double {


return a + b
}
} class Calc {

func add<T: Numeric>(a: T, b: T) -> T {


return a + b
}
}
Generic Type
A Datatype which is capable of holding different kind of
values and perform operations on them.
class Stack<T> {
class Stack {
var itemHolder: [T] = []
var itemHolder: [Int] = []
func push(aItem: T){
func push(aItem: Int){
itemHolder.append(aItem)
itemHolder.append(aItem)
}
}
func pop() {
func pop() {
itemHolder.removeLast()
itemHolder.removeLast()
}
}
}
}

let stringStack = Stack<String>()


let intArray = Array<Int>()
What is Closure? Syntax?
Closure is a block of statements which can be passed as
arguments to a method.

Closures are unnamed functions

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

let sumRes = sumClosure(10,20) // 30


print(sumRes)

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


Does closure capture values?
Yes. Closures capture values which are used in closure
blocks. Use weak or unowned to avoid capturing values.

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is type casting?
Process of converting one datatype value to another type.
Swift doesn’t support implicit type casting as in other
languages.

let ageString: String = "29"


let age: Int = Int(ageString)

ageString is a String, we converted String to Int using type


casting.

Syntax:

let destinationTypeValue = DestinationType(sourceValue)


What is as, is keywords?
as tell to compiler that consider a variable as given type if
that can be considered as given type.

is is type checking operator which checks the variable is of


given type or not.

let aNumber: Any = 10 DY


E D
if aNumber is Int {
R
print(aNumber)
D N
} else if let intValue = aNumber as? Int {
print(intValue)
}

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is Exception Handling? Syntax?
Process of responding and recovering from error conditions
in a program.

Using try, throw, throws, catch we can achieve it.


What is inout parameter?
By default all parameters passed to functions are constants.
If you want to modify the value of a parameter use inout
keyword. So that you can pass their references.

func swap(a: Int, b: Int) {

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)

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is init?
init() is a default constructer to initialise proper default
values to instance variables of Objects.

class Person {
let age: Int
DY
let name: String
E D
init() {
R
age = 29
D N
name = "DNReddi"
}
}

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is deinit? or What is default destructor ?
deinit() is a default destructor of a class where you can
make extra cleanup of resources in a class

deinit {
print("Extra cleanup of memory")
}

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


Convenience vs designated initialisers
Default init methods are the designated initialisers

Convenance initialisers are the custom initialisers which


assigns default values for some properties of a class.
Use convenience keyword to define convenience initialisers.
class Initialisers {
DY
D
var a: Int = 10

E
var b: Int
var c: Int
var d: Int
R
init() {
b = 0
D N
c = 0
d = 0
}

convenience init(tempD: Int) {


self.init()
d = tempD
}
}

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is defer?
Defer is a block of code which gets executed just before
completing the executing of that method.

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

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is Access Specifier ?
Access specifiers specifies the availability or scope of a
variable
What are the access specifiers available?
Open
Public
Internal (Default)
Private
File-Private
What is fileprivate access specifier?
fileprivate is an Access Specifier, the fileprivate variable or
method can be accessed in any where (subclasses,
extensions) in the file where it is declared.

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is private access specifier?
private is an Access Specifier, the private variable or
method can be accessed with in the same block where it is
declared.

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is public ?
Public is an Access Specifier, the public variable or method
can be accessed any where (same block, same file, subclass,
extension, other frameworks)

The public class in a framework can not be extended.

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is open ?
Open is an Access Specifier, the open variable or method can
be accessed any where in the same framework and other
frame works.

The open classes in a framework can be extended.

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


Open vs Public
The open classes in a framework can be extended.

The public class in a framework can not be extended.

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


Higher Order Functions
Higher order functions are used to perform operations on
collections in an optimised way

There are five higher order functions

map
filter DY
reduce
E D
flatmap
R
compactMap
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is map?
Map function is used to perform same operation on all collection
items.

let anArray = [1,2,3,4,5]


//map

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]

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is Filter?
Filter function is used to perform some condition on all
collection items and it returns the element only when the
condition is true.

// Find even numbers in a collection

let anArray = [1,2,3,4,5]


DY
let evenNumbers = anArray.filter{$0 % 2 == 0}
D
print(evenNumbers) // [2,4]
E
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is reduce?
Use reduce to combine all values of a collection to a single
value.

let aCollection = [1,2,3,4,5,6,7,8,9,10]


let res = aCollection.reduce(0) { $0 + $1}
print(res) // 55

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is flatmap?
Flatmap creates a collection from a collection of collections.
Note: One step unwrapping only.

Flatmap unwraps the collection of collections to a collection.

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]]]

let flatMapRes = collectionOfCollections.flatMap { $0 }


print(flatMapRes)
[1, 2, 3, 4, [5, 6, 7], [8, 9]]

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is compactMap?
Compact map removes nil values form a collection and
returns non nil collection.

let four: String? = nil

let coll = ["1", "2", "3", four]

DY
let nonNilCollection = coll.compactMap { $0 }
print(nonNilCollection)
["1", "2", "3"]
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is Protocol?
Protocol is a set of methods and properties declarations
those are expected to be implemented in adopting class.

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()
}

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


Can we declare Properties in Protocol?
Yes we can declare properties in Protocols those can be gettable
or gettable and settable.

Don’t assign value for the properties of Protocol. Values are


expected to be assigned in Adopting class.

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.

@objc protocol ArithmaticProtocol {


var aValue: Int {get set}
var bValue: Int {get set}
DY
var result: Int { get }
E D
R
func add()
func sub()
D N
@objc optional func mul()
@objc optional func div()
func displayResults()
}

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is the Default method types?
By default all methods in a protocol are required. Those are
expected to implement in adopting class.

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


How to adopt Protocols? can we adopt
multiple protocols?
@objc protocol ArithmaticProtocol {
// Properties
var aValue: Int {get set}
// Default required method
func add()

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")
}
}

Adopt multiple protocol by separating with coma(,)

class Calculator: ArithmaticProtocol, AnotherProtocol {


// Implementation goes here
}
Reach me for Offline/Online training @iPhoneDev1990@gmail.com
Advantages of Protocols?
1. To Achieve multiple inheritance
2. To achieve Delegation Design pattern.

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


Can Delegates be strong?
Delegate object must be weak otherwise it creates retain
cycle.

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


Can we extend Protocols?
Protocols can be extended to provide default
implementations.

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is Extension ?
It is a way to add methods to an existing class without
subclassing.

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
}
}

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


Extension vs Inheritance
Extensions Subclassing / Inheritance

Overriding is not allowed in


Methods can be overridden
Extensions

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.

G e t t h e a d d i t i o n a l Need to create subclass


functionality by using the object to get the additional
existing class instances functionality

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is ARC?
ARC is Automatic Reference Counting available in iOS to
manage the memory of the application.

ARC is a compile time mechanism to manage the memory of


an Application.

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

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is retain cycle?
ARC system fails to release the memory of the objects which
are strongly pointing to each other creates a retain cycle.

DY
D
Strong

R E
D N Strong

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


How to avoid retain cycle?
Make one of the association / relationship as weak.

DY
D
Strong

R E
D N Weak

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is weak ?
weak is a keyword which doesn’t increase the reference
count of the object when it is shared or passed as an
argument.

Weak objects must be optionals.

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is unowned?
Unowned is similar to weak but never becomes nil so it
should not be an optional.

Use unowned in closures when you are sure that object


never becomes nil

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


Array vs Set?
Array is an ordered collection of similar / dissimilar elements
Set is an unordered collection of unique elements

Array elements are accessed using index


Set doesn’t contain any index or key to access an element

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

Use Array when order is important


Use Set when order is not important

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


Tuples vs Dictionaries
Tuples groups multiple values together
Dictionary is a collection of key-value pairs

Tuples doesn’t has any methods to perform operations


Dictionary has enough methods to perform operations

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

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is fallthrough?
fallthrough is a keyword used in switch statement.

If fallthrough exists in a successful case it executes the next case


irrespective of the case value match.

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")

print("Case doesn't match")


}

O/P:
This is 10
It is bigger than 10

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


Is optional Enum or Struct ?
Enum

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


How to declare a selector in Swift?
#selector(ClassName.abc)

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


How to check the variable type?
is is an operator which checks the type if a variable

let a = 10

if a is Int {

Y
print("a is an integer”)
}
D D
R E
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


How to call a method with some delay?
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5) {
methodCall()
}

Timer can also be used to call a method with some delay

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is KVO?
Key Value Observing is a process of observing the changes in
a an object / variable.
The changes gets notified through observeValue method.

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


What is KVC?
Key Value Coding is a process of accessing properties of an
Object using keys.

class Mobile: NSObject {


var model: Int = 2018
}
DY
let myMobile = Mobile()
E D
myMobile.model R
myMobile.model = 2019
D N
myMobile.value(forKey: "model")
myMobile.setValue("2019", forKey: "model")

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


How to access Private Variables of a class out side?
You can access private variables using KVC.

DY
E D
R
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


How to write singleton in Swift?
class Database {
static let dbManager: Database = Database()
func save() {

func clean() {
DY
}
E D
}
R
D N
Database.dbManager.save()
Database.dbManager.clean()

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


How do you make sure singleton is created only once?
Don’t provide access to init method. So that other part of the
program can be created object of Database. Use private init to
restrict access.

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

Reach me for Offline/Online training @iPhoneDev1990@gmail.com


DY
E D
ThankR You
D N

Reach me for Offline/Online training @iPhoneDev1990@gmail.com

Вам также может понравиться