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
protocol MyProtocol { | |
var myVar1: String { get set } | |
var myVar2: String { get } | |
} | |
struct MyStruct: MyProtocol { | |
var myVar1 = "" | |
var myVar2 = "" | |
} |
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
var testStruct = MyStruct() | |
testStruct.myVar1 = "test var1" | |
testStruct.myVar2 = "test var2" | |
print(testStruct.myVar1) // "test var1" | |
print(testStruct.myVar2) // "test var2" |
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
var testProtocol: MyProtocol = MyStruct() | |
testProtocol.myVar1 = "test var1" // No error | |
testProtocol.myVar2 = "test var2" // error: cannot assign to property: 'myVar2' is a get-only property |
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
protocol Flyable { | |
/// Limit the speed of flyable | |
var speedLimit: Int { get set } | |
func fly() | |
} | |
extension Flyable { | |
func fly() { |
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
class Bird: Flyable { | |
var speedLimit = 20 | |
} |
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
protocol Flyable { | |
// Make speedLimit only gettable | |
var speedLimit: Int { get } | |
func fly() | |
} | |
class Bird: Flyable { | |
// Make speedLimit private | |
private(set) var speedLimit = 20 |
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
protocol Flyable { | |
/// Limit the speed of flyable | |
var speedLimit: Int { get } | |
func fly() | |
} | |
extension Flyable { | |
func fly() { |
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
protocol Animal { | |
associatedtype FoodType | |
func eat(food: FoodType) | |
} |
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
protocol Animal { | |
var name: String { get } | |
func walk() | |
} | |
class Cow: Animal { | |
let name: String | |
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
protocol Animal { | |
var name: String { get } | |
func walk() | |
associatedtype FoodType | |
func eat(food: FoodType) | |
} | |
class Cow: Animal { |
OlderNewer