Assigning to self Within a Mutating Method

Mutating methods can assign an entirely new instance to the implicit self property. The Point example shown above could have been written in the following way instead:

  1. struct Point {
  2. var x = 0.0, y = 0.0
  3. mutating func moveBy(x deltaX: Double, y deltaY: Double) {
  4. self = Point(x: x + deltaX, y: y + deltaY)
  5. }
  6. }

类方法:

You indicate type methods by writing the static keyword before the method’s func keyword. Classes may also use the class keyword to allow subclasses to override the superclass’s implementation of that method.

  1. class SomeClass {
  2. class func someTypeMethod() {
  3. // type method implementation goes here
  4. }
  5. }
  6. SomeClass.someTypeMethod()

可读写性:

Structures and enumerations are value types. By default, the properties of a value type cannot be modified from within its instance methods.

However, if you need to modify the properties of your structure or enumeration within a particular method, you can opt in to mutating behavior for that method. The method can then mutate (that is, change) its properties from within the method, and any changes that it makes are written back to the original structure when the method ends.

swift语言点评十一-Methods的更多相关文章

  1. swift语言点评二十一-协议

    定义有什么,及哪些必须实现. A protocol defines a blueprint of methods, properties, and other requirements that su ...

  2. swift语言点评四-Closure

    总结:整个Closure的作用在于简化语言表述形式. 一.闭包的简化 Closure expression syntax has the following general form: { () -& ...

  3. swift语言点评十四-继承

    Overriding A subclass can provide its own custom implementation of an instance method, type method, ...

  4. swift语言点评八-枚举

    总结:swift中的枚举可以看作变量可以作为case匹配参数的类 Enumerations 枚举的作用:状态列举与匹配 枚举值与类型 If a value (known as a “raw” valu ...

  5. swift语言点评二

    一.数据类型 1.基础类型的封装 Swift provides its own versions of all fundamental C and Objective-C types, includi ...

  6. swift语言点评十九-类型转化与检查

    1.oc比较: -(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例 -(BOOL) isMemberOfClass: classObj 判断是否是这个 ...

  7. swift语言点评十八-异常与错误

    1.错误类型与枚举的结合 enum VendingMachineError: Error { case invalidSelection case insufficientFunds(coinsNee ...

  8. swift语言点评十七-Designated Initializers and Convenience Initializers

    Swift defines two kinds of initializers for class types to help ensure all stored properties receive ...

  9. swift语言点评十-Value and Reference Types

    结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: f ...

随机推荐

  1. 洛谷P3381 【模板】最小费用最大流(dijstra费用流)

    题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表 ...

  2. Android DrawerLayout设置左右侧滑菜单为全屏

    我们可以在MainActivity中获取屏幕宽度后动态赋值给侧滑菜单. 在oncreate时 DisplayMetrics metric = new DisplayMetrics(); getWind ...

  3. php方法----将数组按照键值进行排序

    将数组按照键值进行排序 array_multisort(array_column($arr,'first'),SORT_ASC,$arr);

  4. javaScript注释 to 颜文字

    将javascript 注释(alert.console)转化为 颜文字语言. http://utf-8.jp/public/aaencode.html

  5. Guitar Pro中文版下载,你想要的,都在这啦!

    我的音乐我做主!Guitar Pro7中文版的发布为更多音乐爱好者带来更多更优质的体验!为帮助所有吉他爱好者学习.绘谱.创作而设计的音乐空间!为前所未有的音乐盛听而震撼,音乐才子,等的就是你! Gui ...

  6. ABBYY迎国庆·庆中秋限时折扣狂潮,再来一波

    继ABBYY 早秋限时活动之后,ABBYY官方为迎国庆,庆中秋,折扣狂潮,又来一波.上次活动由于时间短,任务急,数量少,使得不少小伙伴抱憾而止,选择默默等待良机.现在,良机来了,即便没有上次的打折力度 ...

  7. selenium基础

    浏览器 selenium本质是通过驱动浏览器,完全模拟浏览器的操作,比如跳转.输入.点击.下拉等来拿到网页渲染之后的结果,可支持多种浏览器 官网链接:http://selenium-python.re ...

  8. Windows系统Python环境搭建

    Python下载 下载地址:https://www.python.org/downloads/ 选择需要下载的版本 以Python3.3.7版本为例,下载64位和32位都分别有三种方式,依次是压缩包, ...

  9. Pyhton学习——Day8

    ###########################################max函数#################################################### ...

  10. HDU 2049 不容易系列之(4)——考新郎( 错排 )

    链接:传送门 思路:错排水题,从N个人中选出M个人进行错排,即 C(n,m)*d[m] 补充:组合数C(n,m)能用double计算吗?第二部分有解释 Part 1. 分别求出来组合数的分子和分母然后 ...