Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

protocol oriented programming


///what does protocol extension allows us to do in swift
///it allow to create default function for protocol
protocol Greeting {
    func sayHello()
}


extension Greeting {
    func sayHello() {
        print("Hello World")
    }
}


class SimpleGreeting: Greeting { //No need to impliment the function
    
}

class WishesGreeting: Greeting {
    func sayHello() {
        print("Hello World this is new year")
    }
}


//Other way to create option protocol
@objc protocol ObjcGreeting {
  @objc optional func sayHello()
}


class ObjcSimpleGreeting: ObjcGreeting { //No need to impliment the function
    
}

class ObjcWishesGreeting: ObjcGreeting {
    func sayHello() {
        print("Hello World this is new year")
    }
}




class ProtocolOrientedProgrammingViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = .white

        let wishesGreeting = WishesGreeting();
        wishesGreeting.sayHello()
        
    }
}
Comment

PREVIOUS NEXT
Code Example
Swift :: swift optionals 
Swift :: uicolor gray 
Swift :: Speech recognizer swiftui 
Swift :: swift isMemberOf 
Swift :: ternary operator in swift 
Swift :: swift 5 on return button action 
Swift :: Swift e Over enum Cases 
Swift :: swift increment for loop by 2 
Swift :: How to make dart typing stricter 
Swift :: get device height spritekit 
Swift :: swift variable 
Swift :: cellwidget to setvalue 
Ruby :: ruby delete file 
Ruby :: hashwithindifferentaccess ruby 
Ruby :: rails delete link 
Ruby :: ruby format date time 
Ruby :: ruby get file name 
Ruby :: run a specific migration rails 
Ruby :: ruby check if a file exists 
Ruby :: how to get new line to display in rails 
Ruby :: add column with default value in rails 
Ruby :: for loop ruby 
Ruby :: create a new hash from existing hash ruby 
Ruby :: link to do rails 
Ruby :: ruby create array 
Ruby :: ruby remove value from array 
Ruby :: ruby for 
Ruby :: each_cons with index ruby 
Ruby :: add index in rails 
Ruby :: how to unloack user devise rails 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =