陈高健
谢谢,学到很多
2020-04-04
1
晏书
这个是我第一次留言,学习到现在才发现了swift的神奇之处,并对他产生了主动性学习的动力。
作者回复:加油,相信国内的swift环境会越变越好
2020-04-03
1
Nate Robinson
蛮喜欢老师这个先理论+再实际代码体验的教学模式的,这样的模式比起一边理论,一边代码体验感觉上更让人有了自主学习总结的间隙,有助于消化吸收。
2020-03-16
无问西东
感谢大佬, 全部看完了来评价, 收获很多, 期待老师出新课程
2020-01-06
1
L R
很不错 对swift可以有全面的认识 支持
2019-12-26
1
一个工匠
优秀优秀,很多视频都看了,棒
2019-12-12
1
Life is fantastic
不好意思,前几天学所有权在消化做笔记。今天把这段代码分享给老师和大家,代码来源于互联网,注释为本人注释,如有不对的地方请大家指正,。
prefix operator +☀
infix operator +-☀ : PlusMinusPrecedence
precedencegroup PlusMinusPrecedence {
associativity: none // 结合性(left\right\none)
higherThan: AdditionPrecedence //比谁的优先级高
lowerThan: MultiplicationPrecedence//比谁的优先级低,不能是该模块内部的
assignment: false //如果设置成true,那么遇到可选链的时候该操作符就相当于赋值操作符。否则false就不执行赋值操作。
}
struct Point {
var x: Int, y: Int
static prefix func +☀(point: inout Point) -> Point {
point = Point(x: point.x + point.x, y: point.y + point.y)
return point
}
static func +-☀(left: Point,right: Point) -> Point {
return Point(x: left.x + right.x, y: left.y - right.y)
}
static func +-☀(left: Point?,right: Point) -> Point {
print("+-☀")
return Point(x: left?.x ?? 0 + right.x, y: left?.y ?? 0 - right.y)
}
}
struct Person {
var point: Point //= Point(x: 10, y: 20)
}
var person: Person? //= Person.init()
person?.point +-☀ Point(x: 10, y: 20)//(Point(x: 10, y: 20) +-☀ Point(x: 10, y: 20))
print(person?.point as Any)
var test = Point(x: 10, y: 20)
Point(x: 10, y: 20) +-☀ (+☀test)
作者回复:我把你的例子改了一下,既然是赋值运算,你的实现里明显不是。
infix operator +-☀ : PlusMinusPrecedence
precedencegroup PlusMinusPrecedence {
associativity: none
higherThan: AdditionPrecedence
lowerThan: MultiplicationPrecedence
assignment: true
}
struct Point {
var x: Int, y: Int
static func +-☀(left: inout Point, right: Point) {
left = Point(x: left.x + right.x, y: left.y - right.y)
}
}
struct Person {
var point: Point
}
var person: Person?
person?.point +-☀ Point(x: 10, y: 20)
print(person?.point as Any)
你可以尝试把assignment的true改成false,是会报错的,所以assignment的可选链里的作用如下:
“This proposal quietly drops the assignment modifier that exists on operators today. This modifier had one important function–an operator marked assignment gets folded into an optional chain, allowing foo?.bar += 2 to work as foo?(.bar += 2) instead of failing to type-check as (foo?.bar) += 2. In practice, all Swift operators currently marked assignment are at the equivalent of the Assignment precedence level, so the core team recommends making this optional chaining interaction a special feature of the Assignment precedence group.”
2019-11-21
1
aaaVege💫
写的这么细,不容易
2019-11-16
4
月落泉
讲的很好。为你点赞
作者回复:感谢,努力把每个知识点讲明白
2019-11-02
2
澜奏
老师加油更新哇🤩
编辑回复:有些内容已经录好了,等待极客时间的同学每周陆续更新
2019-10-28
3
编辑推荐
包含这门课的学习路径
iOS工程师
7门课程 31.2w人学习
看过的人还看了