Interview Preparation IOS
Interview Preparation IOS
2. What is protocol?
A protocol defines a blueprint of methods, properties and other requirements that suit a
particular task or piece of functionality. The protocol can be adopted by a class, struct or
enumeration to provide actual implementation of those requirements.
1. A class can adopt any number of protocols, but it can inherit only 1 superclass
2. Multiple inheritance for classes is not allowed in swift
3. Variable properties can be gettable or gettable and settable
4. Protocol must conform to a class, struct or enum to provide actual implementation.
5. Inside protocol definition method can have parameters and return types but not body.
6. Default values can’t be specified for method parameters within protocol definition.
7. A protocol can inherit one or more other protocols and can add other requirements
on top of the requirements it adopts.
8. In protocol extensions we can provide default implementations.
9. How protocol method can be made optional
3. Stored property vs Computed property
1. Stored properties store constant(let) and variable(var) values as a part of instance,
whereas Computed properties calculate a value.
2. Both are usually associated to instance of a particular type.
3. Properties which are associated to type itself are type properties
4. Stored properties of Constant Structure instances (If you create an instance of a
struct and assign that instance to constant, you cannot modify the instance
properties, even if they were declared as variable properties)
5. For Classes if you assign an instance to constant then it will allow to modify it’s
variable properties.
6. Lazy Stored Properties
a. A lazy Stored property is a property whose initial value isn’t calculated until
the first time it is used. You indicate a lazy stored property with lazy modifier
before it’s declaration.
b. Lazy property should be always declared as variable because its initial value
might not be retrieved until after instance initialization completes.
c. Constant properties must always have a value before initialization completes,
therefore they can’t be declared as lazy.
7. Computed properties don’t store a value. Instead, they provide a getter and a
optional setter to retrieve and set other properties and values indirectly.
8. For setter default we have newValue in computed property
9. Read only computed properties (Computed property with only have getter but no
setter)
10. Property Observers
a. Property observers observe and respond to the changes in a property’s value.
b. willSet is called just before the value is stored (default parameter will be
newValue)
c. didSet is called immediately after the newValue is stored (default parameter
will be oldValue)
d. Custom parameter
4. Convenience intializer vs designated intializer vs required intializer
Intialization is the process of creating an instance for a class, struct or enum for use.
1. Intializers are special methods that can be called to create a instance before they are
used for the first time.
2. Swift intializers do not return a value.
3. Instance of class types can also implement deintializer, which performs any custom
cleanup just before an instance of class is deallocated.
4. Classes and structures must set all their stored properties with an initial values
before the time of instance is created. They can’t be left in an indeterminate state.
5. Default property values during property declaration (Assign a default value to the
property during its declaration)
6. Initialization Parameters - are part of intializer definition. They have the same
capabilities and syntax as function and method parameters.
7. Initializers are identified based on the name and type parameters. Argument labels
should be provided otherwise it will lead to compile time errors.
8. Default intializers
a. Swift provides default initializers for classes or structs that provides default
values for all of its properties and doesn’t provide one intializer itself.
9. Memberwise intializers for Structures
a. Unlike default intializers structs receive a memberwise initializer, even if it has
stored properties that don’t have any default values.
b. When you call a memberwise initializer you can omit values for any properties
that have default values.
10. Initializer delegation for value types
a. Initializers can call other initializers to perform a part of an instance
initialization. Avoids duplicate code across multiple initializers
11. Class inheritance and Initialization
a. Designated initializers
i. Designated intializers are primary intializers for the class. A
designated initializer fully initialises all of the class stored properties
and calls an appropriate superclass initializer to continue initialization
process up the superclass chain.
ii. Designated initializers are funnel points through which initialization
takes place and through which the initialization process continues to
its superclass chain.
b. Convenience initializers
i. Convenience initializers are secondary, supporting initializers for the
class.
ii. You can define a convenience initializer to call a designated initializer
of the same class, as the convenience initializer can set some of the
designated initializers parameters to default values.
iii. You don’t have to provide convenience initializers if your class don’t
require them.
iv. Convenience initializers are written in the same style as designated
initializers but with the convenience modifier placed before the init
keyword separated by space.
c. A Designated initializer must call its immediate super class designated
initializer.
d. A Convenience initializer must ultimately call its designated initializer.
e. Two Phase Initialization
i. Each stored property is assigned with an initial value by the class that
introduced it.
ii. Once the initial state of stored property is determined then the second
phase starts. Each class is given the opportunity to customise its
stored properties further before the new instance is considered ready
for use.
iii. In Phase 2 customization will be done at superclass first and then to
subclass
f. Initializer Inheritance and Overriding
i. You will always write the override modifier when overriding a
superclass designated initializer, even if your subclass’s
implementation of the initializer is a convenience initializer.
g. Automatic Initializer Inheritance
i. Assume that you provide default values for all the stored properties in
subclass
1. If your subclass doesn’t define a designated initializer then it
will automatically inherit all of its super class designated
initializers.
2. A subclass can implement a superclass designated initializer
as a subclass convenience initializer as a part of satisfying rule
h. Failable initializers
i. After init keyword we will add a question mark and if there is a failure
we will simply return nil.
ii. These are introduced for numeric type conversions.
iii. A failable initializer can call its super class failable initializer
iv. You can override a failable initializer with non-failable initializer but not
the other way around.
i. Required initializers
i. Write the required modifier before the class initializer to indicate that
every subclass of the class must implement the initializer.
ii. You must write required modifier before every subclass
implementation of the required initializer to indicate that the initializer
requirement applies to further subclasses in the chain.
5. Extensions in Swift
Extensions add functionality to an existing class, struct, enum or protocol types.
This includes the ability to extend types for which you don’t have access to the original
source code.
1. Extensions can add computed instance properties and computed type properties to
existing types.
2. Computed properties are read-only in extensions. They can’t add stored properties.
3. Extensions can add convenience intializers to a class but not designated intializers.
Designated intializers and de-initializers must be provided in original class
implementation.
6. Memory Management in swift
iPhone has 2 ways of storing data - disk and RAM
1. When an app is run on your phone then a file containing all the executable
instructions will be loaded into RAM.
2. A chunk of RAM called heap. Where all the instances of a classes will live.
3. In Swift reference types are allocated in heap but not value types.
4. Memory management is handled by Automatic Reference Counting(ARC)
5. For every new instance there will be a reference count. Reference count 0 indicates
that particular instance is no longer needed. ARC will deallcoate memory for the
instance.
6. Strong, weak and unowned
a. Strong
b. Weak
c. unowned
7. ViewController Lifecycle
1. ViewDidLoad()
a. This method is called only once in the viewController Life cycle
b. Called when all the view are loaded
c. Network call, User Interface and other tasks which are need to be done only
once.
2. viewWillAppear()
a. This method is called everytime before the view is visible and before any
animation is configured.
b. View has bound but no orientation is set.
c. Override this method to perform custom tasks associated with displaying the
view such as hiding fields or disabling actions before the view is visible.
3. viewWillLayoutSubViews()
a. When view bounds change, the view adjusts the position of its subviews.
4. viewDidLayoutSubViews()
a. This method is called after the viewController has been adjusting to its
subview following a change on its bound.
b. Add code here to make changes to subviews after they have been set.
5. viewDidAppear()
a. This method is called after the view is present on the screen.
b. Usually save data to coredata or start Animation or start playing a video or
sound.
6. viewWillDisappear()
a. This method is called before the view is removed from the view hierarchy. The
View is still on the view hierarchy not removed yet.
b. Unload animations, handle times, hide the keyboard, cancelling network
requests, revert any changes to parent UI. Also this is an ideal place to store
state.
7. viewDidDisappear()
a. This method is called after the VC’s view has been removed from the View’s
hierarchy. Use this method to stop listening for notifications or device sensors.
8. Application Life Cycle
The main point of entry of iOS Apps is AppDelegate
1. AppDelegate is a protocol that your app has to implement to get notified about user
events such as app launch, app goes into background or foreground, app is
terminated, a push notification is opened etc.
2. willFinishLaunchingWithOptions
a. This method is intended for initial application setup. Storyboards have already
been loaded at this point but state restoration hasn’t occurred yet.
3. didFinishLaunchingWithOptions
a. This call back method is called when the application has finished launching
and restored state and can do the final initialization such as creating UI.
4. applicationWillEnterForeground
a. Called after didFinishLaunchingWithOptions or if your app becomes active
again after the phone call or other system interruption.
5. applicationDidBecomeActive
a. To finish up the transition to foreground
6. applicationWillResignActive
a. Is called when the app is about to become inactive(when the phone receives
a call or user clicks on home button)
7. applicationDidEnterBackground
a. Is called when your app enters background state after becoming inactive.
b. You will have approximately 5 seconds to run any tasks you need to back
things up incase the app gets terminated later or right after that.
8. applicationWillTerminate
a. Is called when your app is about to be purged from memory. Call any final
cleanup’s here.
9. Class vs Struct
10. Class method vs static method
11. Defer keyword