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")



Thursday, November 26, 2020

 Find the Index of element in Array in swift.

        swift 5 , Swift 4.2 

        As a swift find the element in array without pre-define function.

   
     As a swift find the element in array with pre-define function.

 

Program for array rotation In Swift.

 

METHOD 1 (Using temp array) 



METHOD 2 (Rotate one by one)
















METHOD 3 (A Juggling Algorithm) 




Find the Missing Number

Method 1: This method uses the technique of the summation formula. 

  • Approach: The length of the array is n-1. So the sum of all n elements, i.e sum of numbers from 1 to n can be calculated using the formula n*(n+1)/2. Now find the sum of all the elements in the array and subtract it from the sum of first n natural numbers, it will be the value of the missing element.
  • Algorithm: 
    1. Calculate the sum of first n natural numbers as sumtotal= n*(n+1)/2
    2. Create a variable sum to store the sum of array elements.
    3. Print the missing number as sumtotal – sum




Ios Interview Questions

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