Showing posts with label Functional program. Show all posts
Showing posts with label Functional program. Show all posts

Sunday, 16 September 2018

class vs struct

Class

  • class is reference type.
  • class object is created on Heap memory.
  • For class member variable can initialised directly.
  • class have constructor and destructor methods of all types.
  • class can inherit another class.
  • class are good for larger or complex object.

Struct


  • struct is value type.
  • struct object is created on Stack memory.
  • For struct member vaiable can not initialised directly.
  • struct can't be abstract.
  • stucts are good for small and isolated model object.  


Example of class

created one Student class with name and lname property with init method
class Student{
    var name : String?
    var lname : String?
    init(name : String?,lname : String?) {
        self.name = name
        self.lname = lname
    }

}
- now create one object for Student class called objStudent and print
let objStudent = Student(name: "Pratik", lname: "Lad")
dump(objStudent)

Output :
__lldb_expr_44.Student #0
  name: Optional("Pratik")
    - some: "Pratik"
  lname: Optional("Lad")
    - some: "Lad"

- now one more object create and assign older object of Student class
let obj1Student = objStudent
- now change the name of obj1Student object
obj1Student.name = "Bunty"
- now print both object 
dump(objStudent)

Output :
__lldb_expr_44.Student #0
  name: Optional("Bunty")
    - some: "Bunty"
  lname: Optional("Lad")
    - some: "Lad"

dump(obj1Student)

Output :
__lldb_expr_44.Student #0
   name: Optional("Bunty")
    - some: "Bunty"
   lname: Optional("Lad")
    - some: "Lad"
you can see the objStudent object name also change because class is reference type.

Example of struct

created one Student struct with name and lname property with init method
struct Student{
    var name : String?
    var lname : String?
    init(name : String?,lname : String?) {
        self.name = name
        self.lname = lname
    }

}
- now create one object for Student struct called objStudent and print
let objStudent = Student(name: "Pratik", lname: "Lad")
dump(objStudent)

Output :
__lldb_expr_44.Student #0
   name: Optional("Pratik")
    - some: "Pratik"
   lname: Optional("Lad")
    - some: "Lad"

- now one more object create and assign older object of Student struct
var obj1Student = objStudent
- now change the name of obj1Student object
obj1Student.name = "Bunty"
- now print both object 
dump(objStudent)

Output :
__lldb_expr_44.Student #0
   name: Optional("Pratik")
    - some: "Bunty"
   lname: Optional("Lad")
    - some: "Lad"

dump(obj1Student)

Output :
__lldb_expr_44.Student #0
   name: Optional("Bunty")
    - some: "Bunty"
   lname: Optional("Lad")
    - some: "Lad"
you can see the objStudent and obj1Student object name not change because struct is value type.

Thanks for reading this article.



Monday, 3 September 2018

Enum

Enum is data type for consisting name value like String, Int etc.
For good programer practice code is readable and reusable.
Enum is best for comparing in conditional statement and switch case.

For example if we match string type value like

func getDevice(forDeviceType deviceType : String){
    if deviceType == "iOS"{
        print("iOS")
    }else{
        print("Other device")
    }
}

Now how to use enum string type for above function 
enum enumDeviceType : String {
    case iOS
    case Android
}

func getDevice(forDeviceType deviceType : String){
    if deviceType == enumDeviceType.iOS.rawValue{
        print("iOS device")
    }else{
        print("Other device")
    }

}

another example of enums
enum myEnumList{
    case list1
    case list2
    
    var noOfItems : Int{
        switch self {
        case .list1:
            return 50
        case .list2:
            return 100
        }
    }
}

func listEnumTest(type : myEnumList)
{
    switch type {
    case .list1:
        print("hello list 1 total item is \(type.noOfItems)")
        break
    case .list2:
        print("hello list 2 total item is \(type.noOfItems)")
        break
    }
}

listEnumTest(type: .list1)

you can also watch video 




If you have any trouble in use of enum then pls comment 

Thank you


Thursday, 30 August 2018

Multiple Target XCode

Hello Friends

If you developed an App and you want to create different different settings of app like Local, Dev, Prod environments server url change or multiple app developed with same code with minor changes at that time Multiple Target is use full.

How to create multiple target?

- Select project in navigator and select target

creating new target from xcode app


- Now selecting target and create duplicate target 



- Now rename the target (Target Copy -> TargetPROD)

duplicate target create in xcode


- Then Go To Scheme and select Mange Schemes


- Then Double tap to rename Target name (Target copy -> TargetPROD)

Rename target from project schemes


- Now Target created.
- Now based on target we set preprocessor in Build Settings -> Other Swift Flags

- > Target -> -D DEV
- > TargetPROD -> -D PROD

Other swift flag change from build settings in xcode



- Now add this code and change both target and run

import UIKit

#if DEV
    let baseURL = "Dev base url"
#else
    let baseURL = "Prod base url"
#endif

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        print(baseURL)
    }

}

In Target => Print "Dev base url"
In TargetPROD => Print "Prod base url"

You can change also Info.plist, File, bundle settings etc.


You can also watch video 


If you have any query then comment.

Thank you.



Wednesday, 29 August 2018

Generics in iOS Swift

The main use of Generic is avoid code duplication.
Generic placeholder is defined within angle brackets <>.

For example there are two array with different type.

let arrayOfInt = [5,10,15,20]
let arrayOfString = ["Karan","Ankur","Bunty"]

For print this array of value you can create two function like

func printInt(fromArrayOfInt arrayOfInt:[Int]){
    arrayOfInt.map({print($0)})

}

func printString(fromArrayOfString arrayOfString:[String]){
    arrayOfString.map({print($0)})
}

And use

printInt(fromArrayOfInt: arrayOfInt)
printString(fromArrayOfString: arrayOfString)

Output:

5
10
15
20
Karan
Ankur
Bunty

Now using Generic no need to create two function. We can print all type of array using single function like 


func printArray<T>(fromArray arrayOfValue: [T]){
    arrayOfValue.map({print($0)})
}

and calling is same

printArray(fromArray: arrayOfInt)
printArray(fromArray: arrayOfString)

In order to use Generic with struct, class or enum, you can define the Generic placeholder after the name. 

Github link : Generics

You can also watch video : 



Tuesday, 28 August 2018

Swift Protocol

Protocols are a way of specifying how to use of an object.

In Swift Protocols define interfaces which can be any struct, class or enum.



For use of protocol in swift watch below video





Swift MVVM design pattern


Design pattern is an important part for code reusability.

History
Since the beginning of it's development, iOS has used MVC (Model View Controller) design pattern, which helps the developers to reuse code. It also helps to keep UI and business logic separately.

Why MVVM ?
In MVC, As the View and Controller is combined in the ViewController, the controller overloaded. So in order to reduce the code size in controller, MVVM pattern has been introduced.

In MVVM pattern, ViewController does not interact directly with the Model unlike in MVC. Here, ViewController interacts with ViewModel, and ViewModel interact with the Model.

Model(Database) <-->ViewModel (Business Logic) <--> ViewController (UI/View related code)

There are some other architecture design pattern also there like MVC, MVP, MVVM, Viper etc.

Check out below link for MVVM architecture pattern example in Swift.

https://youtu.be/FBJzPppZLsg