From: acevest Date: Tue, 14 Mar 2017 04:56:37 +0000 (+0800) Subject: swift-subscripts X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zlib_tech.html?a=commitdiff_plain;h=bc9501cb3549c5dc45eda8938a88fcf229a3eab2;p=acecode.git swift-subscripts --- diff --git a/learn/AcePlay/AcePlay.playground/Pages/CollectionTypes.xcplaygroundpage/Contents.swift b/learn/AcePlay/AcePlay.playground/Pages/CollectionTypes.xcplaygroundpage/Contents.swift index 78be21e..f3c040e 100644 --- a/learn/AcePlay/AcePlay.playground/Pages/CollectionTypes.xcplaygroundpage/Contents.swift +++ b/learn/AcePlay/AcePlay.playground/Pages/CollectionTypes.xcplaygroundpage/Contents.swift @@ -2,7 +2,7 @@ import Foundation -var str = "Hello, playground" +var str = "Hello, CollectionTypes" //: [Next](@next) diff --git a/learn/AcePlay/AcePlay.playground/Pages/Methods.xcplaygroundpage/Contents.swift b/learn/AcePlay/AcePlay.playground/Pages/Methods.xcplaygroundpage/Contents.swift index 745b406..09c1ce8 100644 --- a/learn/AcePlay/AcePlay.playground/Pages/Methods.xcplaygroundpage/Contents.swift +++ b/learn/AcePlay/AcePlay.playground/Pages/Methods.xcplaygroundpage/Contents.swift @@ -2,7 +2,7 @@ import Foundation -var str = "Hello, playground" +var str = "Hello, Methods" //: [Next](@next) diff --git a/learn/AcePlay/AcePlay.playground/Pages/Subscripts.xcplaygroundpage/Contents.swift b/learn/AcePlay/AcePlay.playground/Pages/Subscripts.xcplaygroundpage/Contents.swift new file mode 100644 index 0000000..8bf935f --- /dev/null +++ b/learn/AcePlay/AcePlay.playground/Pages/Subscripts.xcplaygroundpage/Contents.swift @@ -0,0 +1,80 @@ +//: [Previous](@previous) + +import Foundation + +var str = "Hello, Subscripts" + +//: [Next](@next) + +// 下标可以定义在类、结构体和枚举中 + +struct TimesTable { + let multiplier: Int + + // subscript(index: Int) -> Int { + // get { + // 返回一个适当的 Int 类型的值 + // } + // set(newValue) { + // 执行适当的赋值操作 + // } + // } + + // 只读下标实现如下 + subscript(index: Int) -> Int { + return multiplier * index + } +} + +let sevenTimesTable = TimesTable(multiplier: 7) +print("six times seven is \(sevenTimesTable[6])") + + + +class Matrix { + let rows: Int + let cols: Int + var grid: [Double] + + init(rows: Int, cols: Int) { + self.rows = rows + self.cols = cols + grid = Array(repeating: 0.0, count: rows * cols) + } + + func indexIsValid(row: Int, col: Int) -> Bool { + return row >= 0 && row < rows && col >= 0 && col < cols + } + + subscript(row: Int, col: Int) -> Double { + get { + assert(indexIsValid(row: row, col: col)) + return grid[row*cols + col] + } + + set { + assert(indexIsValid(row: row, col: col)) + grid[row*cols + col] = newValue + } + } + + var description: String { + var s : String = "" + for i in 0.. + \ No newline at end of file diff --git a/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate b/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate index 5d871e6..e07186e 100644 Binary files a/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate and b/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate differ