Tuesday, December 7, 2021

Ios Interview Questions

 

Automatic reference counting | What is swift ARC | Memory management in swift

Code:-

class Person
{
var name: String
weak var job: Job? // Weak to remove the reference count or Retin count
init(_name: String) {
debugPrint("init method of Person called")
name = _name
}
func printName() {
debugPrint("name is \(name)")
}
deinit {
debugPrint("deinit called for person class")
}
}




class Job
{
var jobDescription: String
weak var person: Person?
init(_jobDescription: String) {
debugPrint("init method of Job called")
jobDescription = _jobDescription
}
deinit {
debugPrint("deinit called for job class")
}
}
if (1 == 1)
{
let objPerson = Person(_name: "codect15")
let objJob = Job(_jobDescription: "swift programmer")
objPerson.job = objJob
objJob.person = objPerson
}

protocol and  Genric Class also Conditional conformance in swift example

import UIKit



protocol Bird {

    func fly()

    

}


// optional protocol

extension Bird {

    func fly() {

        print("fly like a bird")

    }

}

class Parot: Bird {

}


class Crow: Bird {

}

protocol Machine  {

    

    func start()

 

}

class Aerplane: Machine , Bird {

    

     func start() {

        

    }

    func fly() {

        print("fly like an Aeroplane")

    }

    

    

}




let objCrw = Crow()

let objParot = Parot()

let objAero = Aerplane()

var arrOjec : [Bird] = [objCrw, objParot, objAero]

var arrItem = [1, 2, 4]



/// Genric Class

class Bag<T> {

    var item:[T] = []

    

    func addElement(element:T) {

        item.append(element)

    }

    func removeLast()-> T {

        

        return  item.removeLast()

    }

   

   

   

}

/// Genric Class Conditional conformance 

extension Bag where T == Int {

    func getSum()->Int {

        let sum = item.reduce(0, +)

       return sum

    }

}


var objBag = Bag<Int>()

var objBag2 = Bag<String>()


objBag.addElement(element: 23)

objBag.getSum()


for item in arrOjec {

    item.fly()

}


print("Working")



No comments:

Ios Interview Questions

  Automatic reference counting | What is swift ARC | Memory management in swift Code:- class Person { var name: String weak var ...