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编译出错,今天装了个Xcode8.3,indexIsValidForRow
函数放到Matrix
结构体中就可以了。修改的代码如下:
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这个函数 func makeIncrementer() -> ((Int) -> Int) { func addOne(number: Int) -> Int { return 1 + number } return addOne } //increment存放makeIncrementer()返回的函数,即increment就是addOne() var increment = makeIncrementer() //实际就是调用了addOne(),输出结果为8 print(increment(7))
I still don't understand.
Why increment can take an argument?
makeIncrementer () takes NO argument.
因为increment是addOne,addOne是带参数的;makeIncrementer()定义时就时没有参数。
你不要把increment当成makeIncrementer(),increment只是makeIncrementer()返回的一个函数。在Java中可以理解为increment是makeIncrementer()返回的一个对象,区别就是在于在Java中函数不是class,而Swift中函数也算是class,所以在Swift中可以把一个函数作为返回值,也可以作为参数。
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?
//这个函数是用于判断两个数组中是否有相同元素 //<T: Sequence, U: Sequence> 这里用了泛型,指定了参数T和U的类型必须实现Sequence协议,即参数必须是数组或集合等可遍历的 func anyCommonElements<T: Sequence, U: Sequence>(_ lhs: T, _ rhs: U) -> Bool //where子句是对泛型对参数T和U做了约束,调用函数时传递的参数达到以下2个条件才行 //T.Iterator.Element: Equatable 参数T必须实现Equatable协议,实现Equatable协议的类型才可以使用 == 和 != 进行比较 //T.Iterator.Element == U.Iterator.Element 这句要求T和U的类型必须是相同的,例:要么都是字符,要么都是数字。 where T.Iterator.Element: Equatable, T.Iterator.Element == U.Iterator.Element { //这里就是循环对比内容了,一旦发现有相同的元素则返回true for lhsItem in lhs { for rhsItem in rhs { if lhsItem == rhsItem { return true } } } return false } //这里结果是true,因为两个数组中都有3 anyCommonElements([1, 2, 3], [3, 4]) //上面调用时参数时用的数字,下面例子使用的字符,其它实现Equatable协议的类型也可以使用。 //这里结果是false anyCommonElements(["1", "2"], ["3", "7"]) //总结:泛型可以使同一个函数,支持多种类型,而不需要为不同类型分别写一个函数。
关于泛型可以参考:泛型 - 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?