// Please run it with Swift 2.1 and Xcode 7.1
import Foundation
let greeting = "Hello everyone, this is my introduction"
extension Array {
func getTheContentOfArray() -> String {
return String(self).stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "[]"))
}
}
enum Gender {
case male
case female
}
protocol Hobby {
func doSth()
}
struct Coding: Hobby {
func doSth() {
print("I love coding...")
}
}
struct Music: Hobby {
func doSth() {
print("I love music...")
}
}
protocol TWer {
var name: String { get }
var gender: Gender { get }
var graduateTime: String { get }
var beingTWer: String { get }
var favoriteTech: [String] { get }
var booksForNow: [String] { get }
func description()
}
struct ZhuXueliang: TWer {
var name: String
var gender: Gender
var graduateTime: String
var beingTWer: String
var favoriteTech: [String]
var booksForNow: [String]
var myHobby: [Hobby]?
func description() {
basicInfo()
additionInfo()
hobbyInfo()
}
private func basicInfo() {
print("My name is " + self.name + ", Gender: " + String(self.gender) + "; and being a TWer about " + beingTWer)
}
private func additionInfo() {
print("About my favorite tech is " + favoriteTech.getTheContentOfArray())
print("About the books for now is " + booksForNow.getTheContentOfArray())
}
private func hobbyInfo() {
if let hobby = myHobby where hobby.count > 0 {
print("My hobby are: ")
for item in hobby {
item.doSth()
}
} else {
print("I do not have any hobby.")
}
}
}
let zxl = ZhuXueliang(name: "Zhu Xueliang", gender: .male, graduateTime: "2013-07-05", beingTWer: "2 months", favoriteTech: ["iOS", "Swift"], booksForNow: ["Clean Code", "The Docker Book", "深入浅出NodeJs"], myHobby: [Coding(), Music()])
zxl.description()