Property in Swift - Computed property | Property Observers (WillSet & DidSet)

   Computed property -  Computed properties are part of a property type in Swift.  Stored properties are the most common which save and return a stored value  Computed properties calculate (rather than store) a value. A computed property provides a getter and an optional setter to indirectly access other properties and values. Computed property in swift is an interesting topic as you can write code inside a property in swift. Computed properties are provided by classes, structures, and enumerations but stored properties are provided only by classes and structures. This computed property swift tutorial has some tips on how to use the computed property in the right way and avoid the common mistakes that swift programming beginners do while computed property. Example :- Computed property A Calculate Simple Intrest struct CalculateLoan {      var amount : Int      var rate : Int      var years : Int      var simpleInterest: Int {          get {              return ( amount * rate

Custom Higher Order Function Filter Even Number

An Any and AnyObject Difference

Custom Higher Order Function

 

 

 if an Any Declare 


 let anyValues: [Any] = [1, "two", 3, "four", ["name": "praveen"]]


 anyValues.forEach { (anyValue) in

    switch anyValue {

        

    case is Int:

        print("\(anyValue) is an Int!")

    case is String:

        print("\(anyValue) is String!")

    case is [String: Any]:

        print("\(anyValue) is Discnary!")

    default:

        print("\(anyValue) is de!")

        

    }

 }


Output => 


 1 is an Int!

 two is String!

 3 is an Int!

 four is String!

 ["name": "praveen"] is Discnary!


 if an AnyObject Declare 


let anyValues: [AnyObject] = [1, "two", 3, "four", ["name": "praveen"]]


Error==> Type of expression is ambiguous without more context



Custom Higher Order Function Filter Even Number 



 func coustomFilterNumber<T>(_ inputArray: [T], _ condition: (T) -> Int)-> [T]{

    

    var result = [T]()

    

    for element in inputArray{

        if (condition(element) != 0){

            result.append(element)

        }

    }

    

    return result

    

 }


 let nums = [2, 5, 10, 4, 6, 7, 11]

 let num = coustomFilterNumber(nums, {$0 % 2})

 print(num)


 Output -- > [5, 7, 11]



  Custom function to apply multiple conditions?



struct Person {

    let name: String

    let age: Int

    let isEmp: Bool

}



func EmpFilter(_ persons: [Person], _ filters: [(Person) -> Bool]) -> [Person]{

    

    var result: [Person] = []

    

    for person in persons{

        var shouldInc = true

    

        for filter in filters{

            if !filter(person){

                shouldInc = false

                break

            }

        }

        if shouldInc{

            result.append(person)

        }

    }

    return result

}


let people = [

    Person(name: "Alexander", age: 28, isEmp: true),

    Person(name: "Bob", age: 22, isEmp: false),

    Person(name: "Catalina", age: 35, isEmp: true),

    Person(name: "Diana", age: 40, isEmp: false),

    Person(name: "Emly", age: 19, isEmp: true)

]


let filter: [(Person) -> Bool] = [

    {$0.age > 25},

    {$0.isEmp},

    {$0.name.count > 5}

]


let filterPeson = EmpFilter(people, filter)


for feson in filterPeson{

    print("\(feson.name), Age: \(feson.age), Employed: \(feson.isEmp)")

}



Comments