/*
protocol SomeProtocol {
// definisikan protocol di sini
}
struct SomeStructure: FirstProtocol, AnotherProtocol {
// definisikan structure di sini
}
class SomeClass: SomeSuperclass, FirstProtocol, Another Protocol {
// definisikan class di sini
}
// mendefinisikan property requirements
protocol SomeProtocol {
var mustBeSettable: Int {
get {
}
set {
}
}
var doesNotToBeSettable: Int {
get {
}
}
}
protocol AnotherProtocol {
static var property: String {
"Selalu gunakan static saat definisikan property requirements"
}
}
*/
protocol FullyNamed {
var fullName: String {
get
}
}
struct Person: FullyNamed {
var fullName: String
}
let john = Person(fullName: "John Pamintuan")
print(john.fullName)
class Starship: FullyNamed {
var prefix: String?
var name: String
init(name: String, prefix: String? = nil) {
self.name = name
self.prefix = prefix
}
var fullName: String {
return (prefix != nil ? prefix! + " " : "") + name
}
}
var ncc1701 = Starship(name: "Enterprise", prefix: "USS")
print(ncc1701.fullName)
protocol RandGenerator {
func random() -> Double
}
class LinearGenerator: RandGenerator {
var lastRandom = 42.0
let m = 139876.0
let a = 2893.0
let c = 13213.0
func random() -> Double {
lastRandom = ((lastRandom * a + c).truncatingRemainder(dividingBy: m))
return lastRandom / m
}
}
let generator = LinearGenerator()
print(generator.random(), generator.random(), separator: "\t")
protocol Togglable {
mutating func toggle()
}
enum OnOff: Togglable {
case off, on
mutating func toggle() {
switch self {
case .off:
self = .on
case .on:
self = .off
}
}
}
var light = OnOff.off
print("\(light.toggle())", "\(light.toggle())", separator: "\t")
protocol SomeProtocol {
init(some: Int)
}
class SomeClass: SomeProtocol {
required init(some: Int) {
// initializer implementation statements
}
}
protocol tcpprotocol {
init(aprot: Int)
}
class tcpClass: tcpprotocol {
required init(aprot: Int) {
}
}