Search Your Question

Showing posts with label Emproto Technologies. Show all posts
Showing posts with label Emproto Technologies. Show all posts

Difference between category in Objective C and extension Swift?

Ans : 

Differences between Objective-C Category and Extension


1. Factor : Source code
Categories provide a way to add methods to a class even if its source code is not available to you. ex: NSString
Extensions are only possible if the source code is available , because compiler compiles the extension & source code at same time.
2. Factor : Instance variable/Properties
Category : Not possible
Extensions : Possible
3. Factor : Accessibility to Inherited classes.
Category — All methods defined inside a category are also available to the inherited class
Extensions — All properties and methods defined inside a class extension are not even available to inherited class.
When to use : 
Use category when you need to want to add any methods to a class whose source code is not available to you OR you want to break down a class into several files depending on its functionality.
Use class extensions when you want to add some required methods to your class outside the main interface file. Also when you want to modify a publicly declared variable in the main interface file .


Difference between Objective-C category and Swift extension :

Mostly Objective-C category and Swift extension are same. 

Diff : 

In Objective-C category, Computed property/variable can not be declared.
In Swift extension, Computed property(not stored property) can be declared.


What is protocol extension? Why Swift called as Protocol Oriented Language?

Ans : It is same as we extend class to add functions to existing class. Here we can add functions to existing protocol by extending protocol.

protocol proto {
   func add()
}

extension proto {
   func sub()

}

Benefits :

1.

We can also add default body to protocol extension function. So any type which confirm protocol has choice to implement that method or not. In other words we can say that it become optional method. Protocol extension method must have body.

protocol proto {
    func add()
}

extension proto {
    func add()  {
        print("add method called")
    }
    
    func sub() { }
}

class cp : proto {
   
}

let pq = cp()
pq.add()



Output : 
add method called

2. 

We can confirm multiple protocol and we can say that is multiple inheritance.

protocol proto1 {
    func add()
}

protocol proto2 {
    func sub()
}

class cp : proto1,proto2 {
    func add() {
        
    }
    
    func sub() {
        
    }
}

let pq = cp()
pq.add()

3. 

We can make protocol comparable due to protocol extension.

protocol Score: Comparable {
  var value: Int { get }
}

struct RacingScore: Score {
  let value: Int
  
  static func <(lhs: RacingScore, rhs: RacingScore) -> Bool {
    lhs.value < rhs.value
  }

}

4.

Mutating function in protocol to change value of protocol.

Struct is value type. You can only change property of struct only if property declared as var and instance is also var.



So, if we add mutating before func, it allow to change value in struct type. Same thing also can be done with protocol and protocol extension.

5.

There's also obviously anything that you can do with generics in Swift that couldn't be done in Objective C.  So for instance the Indexable protocol could be extended to have a function that returned the index range length which might only apply if the index is an Int like this:

extension Collection where Self.Index == Int
{
  func length () -> Int
  {
     return endIndex - startIndex
  }

}


Due to this very powerful features of protocol in Swift, Swift is called Protocol Oriented  Programming Language.


What is delete rule in Core Data?

Ans : In our database, there may be multiple entities. One entity is connected with another entity in relationship. So deleting one data from one entity may have impact on another entity data depends on relationship or delete rule.

There are 4 delete rules :

  1. No Action
  2. Nullify
  3. Cascade
  4. Deny
Let's take example having one department entity and it is connected to employee entity with relationship as one-to-many. Employee to department entity relationship is one-to-one.

1. No Action : When No Action delete rule is set to relationship, then there will be no impact of deleting record from one entity. If we delete record from department connected to multiple employees, then there be no impact on employee entity. Employee assume that it has still relation with deleted department.

2. Nullify : If delete rule is set to Nullify to the relationship then the destination of the relationship gets nullify.  In our case, if department is deleted, then relationship between employee and department gets nullify. This is default delete rule.

3. Cascade Rule : If we delete department, then its related employees will be deleted if this delete rule is set. This rue is only used when data model has more dependency. 

4. Deny Delete Rule : This is opposite of Cascade Rule. If we set this rule, and we try to delete department which is connected to any of employee, then we are not allowed to delete department.

Depends on project requirement, we can set delete rule.