Search Your Question

SOLID Principle with Example

Ans : 

S - Single responsibility
O - Open closed
L - Liskov Substitution
I - Interface segregation
D - Dependency Inversion

1. Single Responsibility : Each an every class you create/change should have only one responsibility. We have one class which has many methods having different responsibility. If we add all methods in one class then this class looks monster.

Code for Example :

class ViewClaimDetailController {
    
    func getAllClaimDetail() {
        let jsonData = getDataFromAPI()
        let claims = parseJsonAndFillToArray(data : jsonData)
        saveDataToLocalDB(claims: claims)
       
    }
    
    private func getDataFromAPI(){
        
    }
    
    private func parseJsonAndFillToArray(data : Any) {
        
    }
    
    private func saveDataToLocalDB(claims: [Any]) {
        
    }

}

In our above class, there are 3  private methods having different responsibility. It is very difficult for testing this class and methods as its private.

Solution is to make different classes for different behaviour or responsibilities and make one more class and call above classes methods from this class. So code is readable and also testing can be done easily.

class ViewClaimDetailController {
    
    let netcore = NetCore()
    let conversationFactory = ConversationFactory()
    let coredata = coredataController()
    
    func getAllClaimDetail() {
        let jsonData = netcore.getDataFromAPI()
        let claims = conversationFactory.parseJsonAndFillToArray(data : jsonData)
        coredata.saveDataToLocalDB(claims: claims)
    }
}

class NetCore {
    func getDataFromAPI() {
        // save to coredata
    }
}

class ConversationFactory {
    func parseJsonAndFillToArray(data: Any) {
        // save to coredata
    }
}

class coredataController {
    func saveDataToLocalDB(claims: [Any]) {
        // save to coredata
    }

}


2. Open Closed Principle : Classes and Module should be open for extension and closed for modification. According to this principle, we should write such a class or code that should not be change when new requirement comes.

Code for Example :

class Rectangle {
    var width : Double = 0
    var height : Double = 0
    
    init(inWidth: Double, inHeight: Double) {
        self.width = inWidth
        self.height = inHeight
    }
}

class AreaCalc {
    
    func calculateArea(rectangel: Rectangle) -> Double {
        return rectangel.width  * rectangel.height
    }

}

 If we want to calculate area for new shape, then we have to modify existing calculateArea() method like following :

class AreaCalc {
    
    func calculateArea(shape: AnyObject) -> Double {
        if (shape is Rectangle) {
            return shape.width  * shape.height
        } else if (shape is Square) {
            return shape.width * shape.width
        }
    }

}

Again, if want to add area of circle, triangle, we modify method and expand our if....else if... condition.

This is not good according open-closed principle. According to open-closed principle above code should be like following : 

protocol Shape {
    func calculateArea() -> Double
}

class Rectangle: Shape {
    
    var width : Double = 0
    var height : Double = 0
    
    init(inWidth: Double, inHeight: Double) {
        self.width = inWidth
        self.height = inHeight
    }
    
    internal func calculateArea() -> Double {
        return self.width * self.height
    }
}

class Circle: Shape {
    let radius : Double = 0
   
    internal func calculateArea() -> Double {
        return M_PI * radius * radius
    }
}

class AreaCalc {
    
    func area(shape: Shape) -> Double {
        return shape.calculateArea()
    }
}

We can achieve open-closed principle with help of protocol.

3. Liknov's substitution Principle : New derived classes should extend the base classes without changing the base class behaviour. Subclass should override the parent class methods in a way that doesn't break the functionality of base class from client point of view. 

Code for example : 

We have rectangle class in open-closed principle. Assume there it is not confirming Shape protocol.
Now we make square class

class square : Rectangle {
    
    override var width: Double {
        didSet {
            height = width
        }
    }
    
    override var height: Double {
        didSet {
            width = height
        }
    }
}

Now we write test cases :

func testAreaOfRectang1eFor4X3()
{
    let rectangle: Rectangle = Square()
    rectangle.height = 3
    rectangle.width = 4
    let areaCa1cu1ator = AreaCalc()
    let areaOfRectang1e = areaCa1cu1ator.area(rectang1e: rectangle)
    XCTAssertEqua1(areaOfRectang1e , 12, "Area of Rectangle not matching" )
}

Here, rectangle is of square type finding area of rectangle. As we saw override property of square, we can not lost rectangle (base) class definition and we violate liknov principle of Derived class (Square) breaking the parent class (Rectangle) funtionality of caluculating the area. 

Solution of this is only to make protocol and confirm that. That means, we can conclude that violating Liskov’s Principle violates Open Close Principle as well. 

4. Interface segregation principle : 

This principle solves FAT interface problems of Object Oriented Programming. A interface is called FAT when it has too many methods which contains more information than we really want.

Code for example : 

protocol Gesture {
    func didTap()
    func didDoubleTap()
}

class View1 : Gesture {
    
    func didTap() {
        //required this method
    }
    
    func didDoubleTap() {
        // not required this method
    }
}

class View2 : Gesture {
    
    func didTap() {
        // not required this method
    }
    
    func didDoubleTap() {
        //required this method
    }
}

Here view1 required only didTap() method and view2 require only didDoubleTap() method still both class has to implement both methods. So solution, to make 2 protocol and define each method in in each protocol. Class can confirm multiple protocol to achieve functionality. This is rule of interface seggregation. Or we can use @objc to make optional method.

Example 2 : 

class User {
    
    var firstName : String
    var lastName : String
    var imageURL : String
    
    init(firstName : String, lastName: String, imageURL : String) {
        self.firstName = firstName
        self.lastName = lastName
        self.imageURL = imageURL
    }
}

class UserProfileImageView {
    func loadProfilePhoto(user: User) {
        //load user.imageURL
    }
}


Here in loadProfilePhoto method, User instance is passed so all other information except imageURL are also passed. It is not good.

Solution : Use protocol 

protocol UserProfileViewDetails {
    var imageURL: String { get }
}

class User : UserProfileViewDetails {
    
    var firstName : String
    var lastName : String
    var imageURL : String
    
    init(firstName : String, lastName: String, imageURL : String) {
        self.firstName = firstName
        self.lastName = lastName
        self.imageURL = imageURL
    }
}

class UserProfileImageView {
    func loadProfilePhoto(user: UserProfileViewDetails) {
        //load user.imageURL
    }
}

Now the UserProfileImageView’s loadProfileFor(user:UserProfileViewDetails) which is the client has only the imageURL information with it to display the User Profile Image, which agrees with the Interface Segregation Principle.


5. Dependency Inversion Principle  : High level modules should not depend on low level modules both should depend on Abstractions. 

We have used claim details code in "Single Responsibility Principle" code. There ViewClaimController is tightly coupled with another class called CoreDataController to save data into database. But what will happened when same data should also be saved in File System. 

So Solution : 

Code for Example : 

protocol Database {
    func saveDataToLocalDB(claims : [Any])
}

class ViewClaimDetailController {
    
    let database : Database
    
    init(indatabase: Database) {
        database = indatabase
    }
    
    func getAllClaimDetail() {
        let jsonData = getDataFromAPI()
        let claims = parseJsonAndFillToArray(data : jsonData)
        database.saveDataToLocalDB(claims: claims)
    }
}

class coredataController : Database {
    func saveDataToLocalDB(claims: [Any]) {
        // save to coredata
    }
}

class FileSystemController : Database {
    func saveDataToLocalDB(claims: [Any]) {
        // save to coredata
    }
}



Summary : 
  • Each an every class should have only one responsibility
  • Should not modify the existing class for change requirement, rather extent the class
  • Extending or Inheriting a child/derived class should not break any parent or base class functionality
  • The Interface or class API’s to client should have minimum information required by the client
  • Class A should not depend on Class B or vice versa, both should be losely coupled

If you have any comment, question, or recommendation, feel free to post them in the comment section below!   

I have 100 cells. In each cell I have 5 button. How can I check which cell button is clicked?

Ans : 

There are various approach for this problem. But following two approach are better.

Approach-1 : Delegate - Protocol 

Cell : 

protocol CellSubclassDelegate: class {
    func buttonTapped(cell: CellSubclass)
}

class CellSubclass: UITableViewCell {

@IBOutlet var someButton: UIButton!

weak var delegate: CellSubclassDelegate?

override func prepareForReuse() {
    super.prepareForReuse()
    self.delegate = nil
}

@IBAction func someButtonTapped(sender: UIButton) {
    self.delegate?.buttonTapped(self)

}

ViewController:

class MyViewController: UIViewController, CellSubclassDelegate {

    @IBOutlet var tableview: UITableView!

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CellSubclass

        cell.delegate = self


    }

    func buttonTapped(cell: CellSubclass) {
        guard let indexPath = self.tableView.indexPathForCell(cell) else {
            // Note, this shouldn't happen - how did the user tap on a button that wasn't on screen?
            return
        }

        //  Do whatever you need to do with the indexPath

        print("Button tapped on row \(indexPath.row)")
    }


Approach-2 Using Closure:

Cell :

class MyCell: UITableViewCell {
    var button: UIButton!

    var buttonAction: ((Any) -> Void)?

    @objc func buttonPressed(sender: Any) {
        self.buttonAction?(sender)
    }

}


ViewController : 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! MyCell
    cell.buttonAction = { sender in
        // Do whatever you want from your button here.
    }
    // OR
    cell.buttonAction = buttonPressed(closure: buttonAction, indexPath: indexPath) // <- Method on the view controller to handle button presses.

}

No need to explain code. Simple as that.

If you have any comment, question, or recommendation, feel free to post them in the comment section below!


What is Operator overloading?

Ans : 
Operator overloading is the practice of adding new operators and modifying existing ones to do different things. Operators are those little symbols like +*, and /, and Swift uses them in a variety of ways depending on context – a string plus another string equals a combined string, for example, whereas an integer plus another integer equals a summed integer.
To create a new operator, try adding this to a playground:
infix operator **
That’s the exponentiation operator, designed to raise one number to the power of another. Normally we’d use the pow() function for that job, but with operator overloading we can make ** work instead.
Now you need to tell Swift what to do when it sees that operator. For example, when we write something like 2 ** 4 what does that mean?
Syntax of making operator : 
func **(lhs: Double, rhs: Double) -> Double {
    return pow(lhs, rhs)
}
Use : 
let result = 2 ** 4
We can specify associativity and a precedence group also but it is very deep level.
If you have any comment, question, or recommendation, feel free to post them in the comment section below!

If there are two views up and bottom, up-view is 60% of superview and 40% of superview respectively, then what to do with constraint?

Ans : 


Use of multiplier in constraint in iOS
Use of multiplier in constraint


We took 2 views - Orange and Green. 
We set orange view's top, leading, trailing constraint to main view as 0. 
Same, we set green view's bottom, leading, trailing constraint to main views as 0.

Now,
We select height constraint of orange view equal to main view's height. And set multiplier 0.6. It shows ratio of orange view's height to main view's height. We have set 0.6 = 60/100 . So orange view's height become 60% of main view's height.

Set multiplier of height constraint in size inspector
Set multiplier of height constraint in size inspector


Same we have done with green view and set multiplier 0.4 = 40/100 means 40%.

If you have any comment, question, or recommendation, feel free to post them in the comment section below!


What is meaning of _ in Swift func ?

Ans : 


The _ is used to define that the parameter is not named
If you have multiple _ it states that you do not need to name the parameters in your function call
func myFunc(name:String, _ age:String){       }

myFunc(Milo", "I'm a really old wizard")
If you do not use the underscore you would use 
myFunc(Milo, age: "I'm a really old wizard")
The _ is not necessary in function calls. It is just used to indicate that something does not to have a name.



Explain application states in iOS

Ans : 

There are 5 application states :

  1. Not Running
  2. Inactive
  3. Active
  4. Background
  5. Suspended.
iOS Application states
Application States


iOS Application Lifecycle : 

When an iOS app is launched the first thing called is

Initialise :

application: willFinishLaunchingWithOptions:-> Bool.
This method is intended for initial application setup. Storyboards have already been loaded at this point but state restoration hasn’t occurred yet.

Launch :

application: didFinishLaunchingWithOptions: -> Bool is called next. This callback method is called when the application has finished launching and restored state and can do final initialisation such as creating UI.

applicationWillEnterForeground: is called after application: didFinishLaunchingWithOptions: or if your app becomes active again after receiving a phone call or other system interruption.

applicationDidBecomeActive: is called after applicationWillEnterForeground: to finish up the transition to the foreground.

Termination :

applicationWillResignActive: is called when the app is about to become inactive (for example, when the phone receives a call or the user hits the Home button).

applicationDidEnterBackground: is called when your app enters a background state after becoming inactive. You have approximately five seconds to run any tasks you need to back things up in case the app gets terminated later or right after that.

applicationWillTerminate: is called when your app is about to be purged from memory. Call any final cleanups here.


Both application: willFinishLaunchingWithOptions: and application: didFinishLaunchingWithOptions: can potentially be launched with options identifying that the app was called to handle a push notification or url or something else. You need to return true if your app can handle the given activity or url.

Above all methods are defined in UIApplicationDelegate and AppDelegate.swift has to implement these methods.

If you have any comment, question, or recommendation, feel free to post them in the comment section below!

What is app group capability in iOS?

Ans : 

If we ever needed to share data between your iOS apps and/or your application targets/extension/widgets , there’s a very easy way to do this using app groups. App group capability introduced with iOS 8.
To do so, we can enable the 'App Group' Capability.
  1. In Xcode’s Project Navigator, Click the 'Project Icon'
You can then use NSUserDefaults initWithSuiteName:.
NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.example.app"];
Then set a value in one target…
[sharedDefaults setObject:value forKey:@"key"];
[sharedDefaults synchronize];
And read the associated value in the other target…
NSString *value = [sharedDefaults stringForKey:@"key"];

We can make file observer to be notified for changing value instantly. We can put observer on userdefault.plist file and being notified for some value changed in other application.


Note : BBPortal is example of this.


Can you search your app content In spotlight (search bar)?

Ans : 


Using spotlight search, here we can find loan no 14356 in our application and redirect to view details page of this loan number.

Apple offers us many iOS features that can boost our apps visibility even when the user is not using it. For example, features like Spotlight, Today Widget, iMessage, Push Notifications, Siri and so on...

Spotlight is a super-powerful search feature in iOS that searches through the contents of your installed apps that support it.

If we want our app content to appear in Spotlight, we need to use the CoreSpotlight framework. For creating searchable content we will need to be familiar with:

  • CSSearchableItemAttributeSet - specifies the properties that you want to appear in Spotlight (i.e. title, contentDescription, thumbnail). 
  • CSSearchableItem - this class represents the search item. We can assign a unique identifier for referring the object, a domain to manage the items in groups and attributeSet where we pass the CSSearchableItemAttributeSet that we have created for this object. 
  • CSSearchableIndex - a class that is responsible for indexing the content on Spotlight. It requires an array of CSSearchableItem.
Let's understand through some coding :

We have array of favourite books : 

var favorites = [Int]()   ->   This array contains int id of favourite book.

To enable searching content, we have to add our favourite list in spotlight content like following :

First import ,
import CoreSpotlight 
import MobileCoreServices

When new item added to favourite, it should be also added using following custom function :


func index(item: Int) {
    let book = books[item]

    let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
    attributeSet.title = book[0]
    attributeSet.contentDescription = book[1]

    let item = CSSearchableItem(uniqueIdentifier: "\(item)", domainIdentifier: "com.iosiqabooks", attributeSet: attributeSet)
    CSSearchableIndex.default().indexSearchableItems([item]) { error in
        if let error = error {
            print("Indexing error: \(error.localizedDescription)")
        } else {
            print("Search item successfully indexed!")
        }
    }
}

Understanding above code :

Let's create CSSearchableItemAttributeSet object. This attribute set can store lots of information for search, including a title, description and image, as well as use-specific information such as dates (for events), camera focal length and flash setting (for photos), latitude and longitude (for places), and much more.

Regardless of what you choose, you wrap up the attribute set inside a CSSearchableItem object, which contains a unique identifier and a domain identifier. The former must identify the item absolutely uniquely inside your app, but the latter is a way to group items together. Grouping items is how you get to say "delete all indexed items from group X" if you choose to, but in our case we'll just use "com.iosiqaswift" because we don't need grouping. As for the unique identifier, we can use the project number.

To index an item, you need to call indexSearchableItems() on the default searchable index of CSSearchableIndex, passing in an array of CSSearchableItem objects. This method runs asynchronously, so we're going to use a trailing closure to be told whether the indexing was successful or not.

By default, search item expiry of 1 month. After 1 month, it is deleted from spotlight. We can manually set expiration date like :

item.expirationDate = Date.distantFuture : It will never expire.

If we want to remove content from spotlight or to disable search item for spotlight,

func deindex(item: Int) {
    CSSearchableIndex.default().deleteSearchableItems(withIdentifiers: ["\(item)"]) { error in
        if let error = error {
            print("Deindexing error: \(error.localizedDescription)")
        } else {
            print("Search item successfully removed!")
        }
    }
}

--------

We can now code for tapping on search result interaction in app delegate :

import CoreSpotlight in AppDelegate.swift.

We have to add following AppDelegate method :

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    if userActivity.activityType == CSSearchableItemActionType {
        if let uniqueIdentifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String {
            if let navigationController = window?.rootViewController as? UINavigationController {
                if let viewController = navigationController.topViewController as? ViewController {
                    viewController.readBook(Int(uniqueIdentifier)!)
                }
            }
        }
    }

    return true
}

Credit : HackingWithSwift