-
Protocol
Definition: protocol— allow you to specify and work with the interface of a type without knowing the type itself.
-
basic example
import Cocoa
protocol TabularDataSource {
var numberOfRows: Int {get}
var numberOfColumns: Int {get}
func labelForRow(row:Int) -> String
func labelForColumn(column: Int)->String
func itemForRow (row:Int ,column:Int)->Int
}
func padding(amount:Int) -> String{
var paddingString = ""
for _ in 0..<amount{
paddingString += " "
}
return paddingString
}
func printTable(dataSource:TabularDataSource){
//Create arrays of the row and column labels
let rowLabels = (0..<dataSource.numberOfRows).map{dataSource.labelForRow($0)}
let columnLabels = (0..<dataSource.numberOfColumns).map{dataSource.labelForColumn($0)}
//Create an array of the width of each row
let rowLabelWidths = rowLabels.map{$0.characters.count}
//Determine length of longest row
guard let maxRowLabelWidth = rowLabelWidths.maxElement() else {
return
}
//Create first row containing column headers
var firstRow:String = padding(maxRowLabelWidth) + " |"
//keep track of the width of each column
var columnWidths = [Int]()
for columnLabel in columnLabels{
let columnHeader = " \(columnLabel) |"
firstRow += columnHeader
columnWidths.append(columnHeader.characters.count)
}
print(firstRow)
for i in 0..<dataSource.numberOfRows{
//Pad the row label out so they are all the same length
let paddingAmount = maxRowLabelWidth - rowLabelWidths[i]
var out = rowLabels[i] + padding(paddingAmount) + " |"
//Append each item in this row to our string
for j in 0..<dataSource.numberOfColumns{
let item = dataSource.itemForRow(i, column: j)
let itemString = "\(item) |"
let paddingAmount = columnWidths[j] - itemString.characters.count
out += padding(paddingAmount) + itemString
}
print(out)
}
}
struct Person {
let name: String
let age:Int
let yearsOfExperience : Int
}
struct Department:TabularDataSource{
let name: String
var people = [Person]()
init(name: String){
self.name = name
}
mutating func addPerson(person:Person){
people.append(person)
}
var numberOfRows: Int{
return people.count
}
var numberOfColumns: Int{
return 2
}
func labelForColumn(column: Int) -> String {
switch column {
case 0 : return "Age"
case 1 : return "Years of Experience"
default: fatalError("Invalid column!")
}
}
func labelForRow(row: Int) -> String {
return people[row].name
}
func itemForRow(row: Int, column: Int) -> Int {
let person = people[row]
switch column {
case 0 : return person.age
case 1 : return person.yearsOfExperience
default: fatalError("Invalid column!")
}
}
}
var department = Department(name: "Engineering")
department.addPerson(Person(name:"Joe",age: 30,yearsOfExperience: 6))
department.addPerson(Person(name:"Karen",age: 40,yearsOfExperience: 18))
department.addPerson(Person(name:"Fred",age: 50,yearsOfExperience: 20))
printTable(department)
- usage summation
<small> In order to give a method the right data to process ,we let the data to confirm to a designed protocol.When the data come out from the protocol, it can fit for the method and then the method process is well-functioned as before </small>
网友评论