Swift51.com
Daniel 头像
Daniel  2017-03-23 13:20

苹果Mac Pro垃圾桶 最低配的ME253CH

回复:61  查看:21821  
Swift 头像
Swift  2017-03-29 18:53
@Daniel
I have difficulty to open the site that you said.
是的,需要用VPN才行。
Daniel 头像
Daniel  2017-04-01 01:25
@Swift
@Daniel
I have difficulty to open the site that you said.
是的,需要用VPN才行。

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.

Swift 头像
Swift  2017-04-01 11:36

@Daniel 你能访问就好。这里说的VPN指网络代理,帮你访问一些比较慢或无法访问的网站。

Daniel 头像
Daniel  2017-04-02 23:29

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?

Swift 头像
Swift  2017-04-04 17:16
我2014年开始接触Swift,那时也只是学了点基础语法,没有学习iOS的开发,而且现在语法有变动,还得重新学习。所以我跟你一样是个新手,所以目前给不了你什么建议。
后续网站会更新一些视频教程,希望能够对你有用。
Daniel 头像
Daniel  2017-04-05 22:51
@Swift
我2014年开始接触Swift,那时也只是学了点基础语法,没有学习iOS的开发,而且现在语法有变动,还得重新学习。所以我跟你一样是个新手,所以目前给不了你什么建议。
后续网站会更新一些视频教程,希望能够对你有用。

I tried the sample code in [Subscript Options] of [The Swift Programming Language (Swift 3.1)], I had compilation errors.

Can you help debugging ?

Swift 头像
Swift  2017-04-05 23:23
@Daniel 把代码发出来看看(通过点击红色的Swift图标插入程序代码),我试一下看会不会出错。
Daniel 头像
Daniel  2017-04-06 05:35
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
Swift 头像
Swift  2017-04-08 13:10

@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


Daniel 头像
Daniel  2017-04-09 18:25

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.苹果Mac&nbsp;Pro垃圾桶&nbsp;最低配的ME253CH苹果Mac&nbsp;Pro垃圾桶&nbsp;最低配的ME253CH苹果Mac&nbsp;Pro垃圾桶&nbsp;最低配的ME253CH

Swift 头像
Swift  2017-04-11 20:13

你这还要等很久啊,明年就得学Swift4了。

Daniel 头像
Daniel  2017-04-12 08:16
I have no need to rush. I don't want to buy a current Mac Pro which will not be manufactured soon because a future Mac Pro is completely different from a current one. It is difficult to get the current model repaired in the future due to the eventual lack of its spare parts.
Also I need to spend about RMB50,000 to fix my teeth this year.

dkychang@qq.com

Swift 头像
Swift  2017-04-12 22:10
是的,不急的话可以等下,就像iPhone8一样,值得期待。
Daniel 头像
Daniel  2017-04-24 10:17
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?

			
Swift 头像
Swift  2017-04-24 13:16
//返回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))
Daniel 头像
Daniel  2017-04-24 20:12

I still don't understand. 

Why increment can take an argument? 

makeIncrementer () takes NO argument.

Swift 头像
Swift  2017-04-24 22:02

因为increment是addOne,addOne是带参数的;makeIncrementer()定义时就时没有参数。

你不要把increment当成makeIncrementer(),increment只是makeIncrementer()返回的一个函数。在Java中可以理解为increment是makeIncrementer()返回的一个对象,区别就是在于在Java中函数不是class,而Swift中函数也算是class,所以在Swift中可以把一个函数作为返回值,也可以作为参数。

Daniel 头像
Daniel  2017-04-28 08:55

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?


Swift 头像
Swift  2017-04-29 17:47
//这个函数是用于判断两个数组中是否有相同元素
//<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 教程

Daniel 头像
Daniel  2017-04-30 06:38

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?