Last active
October 16, 2021 09:07
-
-
Save deathlezz/bf5733bab6b8d414aaff829ca51352de to your computer and use it in GitHub Desktop.
Simple use of protocols in Swift 5.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Simple use of protocols | |
// | |
// basic protocol | |
protocol Info { | |
var name: String { get set } | |
} | |
struct User: Info { | |
var name: String | |
} | |
let Josh = User(name: "Josh") | |
print(Josh.name) // Josh | |
// protocol inheritance | |
protocol Pizza { | |
var size: String { get set } | |
} | |
protocol Burger { | |
var price: Double { get set } | |
} | |
protocol Merge: Pizza, Burger { } | |
struct Order: Merge { | |
var size: String | |
var price: Double | |
} | |
let burger = Order(size: "big", price: 4.99) | |
print(burger.size, burger.price) // big 4.99 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment