Thursday, May 24, 2018

Typical iOS practical test - interview

In this tutorial we will learn how to make an App quickly in practical interview test and stand out of crowd by doing some extra good code. Here is a list of things you could learn from this code base.

  • use of closures
  • lazy loading of images using OperationQueue
  • lazy variable use
  • unit test case writing
  • protocol use in swift
  • DispatchQueue  async use
  • class extension
  • Custom UITableViewCell
  • operationQueue handling
  • use of generics
  • subclass use of Operation
  • decodable and decoder use- Easy way to parse JSON
  • use of Url session
  • overriding of methods and variables
  • enum creation and use.


Here is the final output of this tutorial.



Lets start coding.
Here is a link which we are going to parse. It has list of top free apps from the iTunes app store. So here you can see the data which we need is in array called "Results" and that is under dictonary with key "Feed". we are going to parse and build the array of result array.



So we have a view-controller with Tableview and custom cell to show the image and text details of the app. we take UINavigationController and set our viewcontroller as root viewcontroller. In viewcontroller we have tableview and custom cell which has two labels, UIImageView and one activity indicator.


Now we need one ViewModel as middle to communicate with our model and update ourUI. ViewModel is requesting for the data to model and will be holding the result along with model object. Here you see ViewModel is actually cleaver than view-controller and our vc is dumb, but filled with all the equipments it requires. here you see viewmodel is actual  mind of our viewcontroller and control every aspect of data.


Now its time to create a model it actually holds the actual business logic and understanding what and how to call the web service, using which class too. after that it parse the result using jsonparse class which is leveraging two new swift features. one is generics and other is decodable.





Now to create request we have one class as RederRequest and it has actual url path where we need to connect at this point of time we do not require to send any data, if we need that that we can add that in the same class under url content function.


Decodable is useful to convert the response to data-class. Here AppList is a data class or entity and there is no need to map using 'valueForKey:'


We get the response and in response we have url of app image instead of actual image so we will implement lazy loading using operation to download the image. First we will ask for the images which are currently visible on the UI. And as user moves to new list we will ask for that images.
Here is a code of image downloader to download the image asynchronous. We will call this code from model to download.


This is the implementation of ImageDownloader class it download images and save as data in the AppInfo object and update the status of image.



Now Lets move to XCTest cases.
Here we will write some test cases. There are two functions written to cover the code and once we run it we can see how much code is actually covers.


Check the code coverage image where you can see some number highlighted in right side it shows how many times this code ran last time. 'appdescription' function has been called 19 time last time the code executed.


This is the find consolidated coverage of our app it is actually around 74%. An app should have at least 80% of coverage to cover all major scenarios. we are short to 6%.

Download full code from Github https://github.com/it-sam/iOS-practical

Friday, July 14, 2017

Swift access modifiers

Swift access modifiers

Swift access modifiers

Open:
open classes and class members can be subclassed and overridden both within and outside the defining module (target). Open access is the highest (least restrictive) access level.

Public:
public classes and class members can only be subclassed and overridden within the defining module (target).

Internal:
Enables an entity to be used within the defining module (target). You typically use internal access when defining an app’s or a framework’s internal structure.

FilePrivate:
Restricts the use of an entity to its defining source file. You typically use fileprivate access to hide the implementation details of a specific piece of functionality when those details are used within an entire file.

Private:
Restricts the use of an entity to its enclosing declaration. You typically use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration. private access is the lowest (most restrictive) access level.

Final:
The final keyword is not an access level but you can add it to any of the access levels (apart from open) to prevent subclassing or overriding.


Now the question is What does it mean by same module?
Let’s understand it by example: many times we subclass UITextField (class), which is a part of UIKIT (Different module) but we can do this because it is defined as open. Same applies for class member and methods.

@available(iOS 2.0, *)
open class UITextField : UIControl, UITextInput, NSCoding, UIContentSizeCategoryAdjusting {
...
...
}

UITextField: Swift access modifiers

Some other observations: (I have checked it with swift 3.0.)

In order to apply the open and public access modifiers we need to inherit our class from NSObject. Or we may get below error while using outside of class.
'openClass' initializer is inaccessible due to 'internal' protection level.
'publicClass' initializer is inaccessible due to 'internal' protection level.
'fileprivateFunc' is inaccessible due to 'fileprivate' protection level

We cannot inherit public class outside the module. We get below error:
Cannot inherit from non-open class 'publicClass' outside of its defining module

We cannot access internal method or object outside the module.
'internalObject' is inaccessible due to 'internal' protection level

In other file or module,We get below error when we try to create object of FilePrivate class and private class respectively.
Use of undeclared type 'fileprivateClass'
Use of undeclared type 'privateClass'

In same file, we can inherit or create object of private class as below and we can access all class members and methods which are not private.

fileprivate var privateClassObject = privateClass()

private class IprivateClass :privateClass {
}

Here is a link of apple document for more details.