Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Created January 18, 2016 19:33
Show Gist options
  • Save aaronksaunders/5a7b771a223ceb0561e2 to your computer and use it in GitHub Desktop.
Save aaronksaunders/5a7b771a223ceb0561e2 to your computer and use it in GitHub Desktop.
//
// CLASSES and PROTOCOLS
//
// References:
// - http://nshipster.com/swift-documentation/
// - http://books.aidanf.net/learn-swift/protocols
//
import UIKit
//
// This is like an interface in java, if you implement a class or struct with
// this protocol, then it must provide an implementation for the specified
// behaviors
//
/**
must have a function that describes the class or struct
*/
protocol Describable {
func desc() -> String
}
/**
must have a function(s)
* name
* sayHello
* sayGoodbye
*/
protocol Dialect {
var name:String {get set}
func sayHello()->String
func sayGoodbye()->String
}
protocol Language:Dialect,Describable {
}
/**
this must provide the functions outlined in the protocols
*/
class Spanish:Language {
var hello:String
var goodbye:String
var name:String = "Spanish"
/**
- Parameter hello
- Parameter goodbye
*/
init (hello:String,goodbye:String) {
self.hello = hello
self.goodbye = goodbye
}
func sayGoodbye() -> String {
return goodbye
}
func sayHello() -> String {
return hello
}
func desc() -> String {
return "\(name) Language"
}
}
/**
this must provide the functions outlined in the protocols
*/
class French:Language {
var hello:String
var goodbye:String
var name:String = "French"
/**
- Parameter hello
- Parameter goodbye
*/
init (hello:String,goodbye:String) {
self.hello = hello
self.goodbye = goodbye
}
func sayGoodbye() -> String {
return goodbye
}
func sayHello() -> String {
return hello
}
func desc() -> String {
return "\(name) Language"
}
}
var spanish = Spanish(hello:"hola", goodbye:"adiós")
print(spanish.desc())
print(spanish.sayGoodbye())
var french = French(hello:"Salut", goodbye:"Au revoir")
print(french.desc())
print(french.sayGoodbye())
/// A individual that speaks a specific language and can say hello and goodbye
class Person:Describable {
/// language they speak
let speaks:Language
/// the person's name
let name:String
/**
Initializes the person object
- Parameter name: The style of the bicycle
- Parameter speaks: the class for the language they speak
*/
init(name:String, speaks:Language) {
self.speaks = speaks
self.name = name
}
/**
- Returns: string for hello
*/
func sayHello() -> String {
return (speaks.sayHello())
}
/**
- Returns: string describing the person object
*/
func desc() -> String {
return "My name is \(name) and I speak \(speaks.desc())"
}
}
var aaron = Person(name:"Aaron Saunders", speaks:french);
print("\(aaron.name) says hello '\(aaron.sayHello())'")
aaron.desc()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment