What is VPN?
Your quoted site https://swift.sandbox.bluemix.net/#/repl will work on the following browers:
360極速瀏覽器, 2144瀏覽器, Google Chrome瀏覽器, 搜狗高速瀏覽器 及 UC瀏覽器.
Thank you very much.
Since you have learned Swift (編程語言) more than a couple of years already, what suggestions and guidance will you give me for learning the language?
My first and current reading is [The Swift Programming Language (Swift 3.1)] with the help of the IBM Swift Sandbox interpreter. What else will you suggest?
I tried the sample code in [Subscript Options] of [The Swift Programming Language (Swift 3.1)], I had compilation errors.
Can you help debugging ?
struct Matrix { let rows: Int, columns: Int var grid: [Double] init(rows: Int, columns: Int) { self.rows = rows self.columns = columns grid = Array(repeating: 0.0, count: rows * columns) } func indexIsValid(row: Int, column: Int) -> Bool { return row >= 0 && row < rows && column >= 0 && column < columns } subscript(row: Int, column: Int) -> Double { get { assert(indexIsValid(row: row, column: column), "Index out of range") return grid[(row * columns) + column] } set { assert(indexIsValid(row: row, column: column), "Index out of range") grid[(row * columns) + column] = newValue } } } var matrix = Matrix(rows: 2, columns: 2) matrix[0, 1] = 1.5 matrix[1, 0] = 3.2 func indexIsValidForRow(row: Int, column: Int) -> Bool { return row >= 0 && row < rows && column >= 0 && column < columns } let someValue = matrix[2, 2] // this triggers an assert, because [2, 2] is outside of the matrix bounds
@Daniel 之前用Xcode7.1編譯出錯(cuò),今天裝了個(gè)Xcode8.3,indexIsValidForRow
函數(shù)放到Matrix
結(jié)構(gòu)體中就可以了。修改的代碼如下:
struct Matrix { let rows: Int, columns: Int var grid: [Double] init(rows: Int, columns: Int) { self.rows = rows self.columns = columns grid = Array(repeating: 0.0, count: rows * columns) } func indexIsValid(row: Int, column: Int) -> Bool { return row >= 0 && row < rows && column >= 0 && column < columns } subscript(row: Int, column: Int) -> Double { get { assert(indexIsValid(row: row, column: column), "Index out of range") return grid[(row * columns) + column] } set { assert(indexIsValid(row: row, column: column), "Index out of range") grid[(row * columns) + column] = newValue } } func indexIsValidForRow(row: Int, column: Int) -> Bool { return row >= 0 && row < rows && column >= 0 && column < columns } } var matrix = Matrix(rows: 2, columns: 2) matrix[0, 1] = 1.5 matrix[1, 0] = 3.2 let someValue = matrix[1, 1] // let someValue = matrix[2, 2] // this triggers an assert, because [2, 2] is outside of the matrix bounds
You are great!
Recently I bought 3 electronic books, but I will wait to read them until I purchase a new Mac Pro next year when it is available.
dkychang@qq.com
Functions are a first-class type. This means that a function can return another function as its value:
func makeIncrementer() -> ((Int) -> Int) { func addOne(number: Int) -> Int { return 1 + number } return addOne } var increment = makeIncrementer() print(increment(7))
Swift is not easy, I don't understand the above code. Do you?
//返回addOne這個(gè)函數(shù) func makeIncrementer() -> ((Int) -> Int) { func addOne(number: Int) -> Int { return 1 + number } return addOne } //increment存放makeIncrementer()返回的函數(shù),即increment就是addOne() var increment = makeIncrementer() //實(shí)際就是調(diào)用了addOne(),輸出結(jié)果為8 print(increment(7))
I still don't understand.
Why increment can take an argument?
makeIncrementer () takes NO argument.
因?yàn)閕ncrement是addOne,addOne是帶參數(shù)的;makeIncrementer()定義時(shí)就時(shí)沒有參數(shù)。
你不要把increment當(dāng)成makeIncrementer(),increment只是makeIncrementer()返回的一個(gè)函數(shù)。在Java中可以理解為increment是makeIncrementer()返回的一個(gè)對(duì)象,區(qū)別就是在于在Java中函數(shù)不是class,而Swift中函數(shù)也算是class,所以在Swift中可以把一個(gè)函數(shù)作為返回值,也可以作為參數(shù)。
func anyCommonElements<T: Sequence, U: Sequence>(_ lhs: T, _ rhs: U) -> Bool where T.Iterator.Element: Equatable, T.Iterator.Element == U.Iterator.Element { for lhsItem in lhs { for rhsItem in rhs { if lhsItem == rhsItem { return true } } } return false } anyCommonElements([1, 2, 3], [3])Another piece of code that I don't understand, maybe you can explain?
//這個(gè)函數(shù)是用于判斷兩個(gè)數(shù)組中是否有相同元素 //<T: Sequence, U: Sequence> 這里用了泛型,指定了參數(shù)T和U的類型必須實(shí)現(xiàn)Sequence協(xié)議,即參數(shù)必須是數(shù)組或集合等可遍歷的 func anyCommonElements<T: Sequence, U: Sequence>(_ lhs: T, _ rhs: U) -> Bool //where子句是對(duì)泛型對(duì)參數(shù)T和U做了約束,調(diào)用函數(shù)時(shí)傳遞的參數(shù)達(dá)到以下2個(gè)條件才行 //T.Iterator.Element: Equatable 參數(shù)T必須實(shí)現(xiàn)Equatable協(xié)議,實(shí)現(xiàn)Equatable協(xié)議的類型才可以使用 == 和 != 進(jìn)行比較 //T.Iterator.Element == U.Iterator.Element 這句要求T和U的類型必須是相同的,例:要么都是字符,要么都是數(shù)字。 where T.Iterator.Element: Equatable, T.Iterator.Element == U.Iterator.Element { //這里就是循環(huán)對(duì)比內(nèi)容了,一旦發(fā)現(xiàn)有相同的元素則返回true for lhsItem in lhs { for rhsItem in rhs { if lhsItem == rhsItem { return true } } } return false } //這里結(jié)果是true,因?yàn)閮蓚€(gè)數(shù)組中都有3 anyCommonElements([1, 2, 3], [3, 4]) //上面調(diào)用時(shí)參數(shù)時(shí)用的數(shù)字,下面例子使用的字符,其它實(shí)現(xiàn)Equatable協(xié)議的類型也可以使用。 //這里結(jié)果是false anyCommonElements(["1", "2"], ["3", "7"]) //總結(jié):泛型可以使同一個(gè)函數(shù),支持多種類型,而不需要為不同類型分別寫一個(gè)函數(shù)。
關(guān)于泛型可以參考:泛型 - Swift 3.0 教程
I know the results of the examples because I tried the IBM compiler. But the terms (Sequence, Iterator and Equatable) have never been explained in the document [The Swift Programming Language (Swift 3.1)]?
Have you read other documents?