百日学 Swift(Day 14) – Swift 复习 第 2 天

百日学 Swift(Day 14) – Swift 复习 第 2 天

1. Functions - 函数

  • 参数(参数标签)及其类型
  • 返回值及其类型

2. Optionals - 可选项

  • ?!的使用
  • 强制解包、隐含解包
  • 程序员要对程序中的每个!负责!!!!

3. Optional chaining - 可选链

  • 链式应用中遇到?就先判断,是nil就忽略其他部分,不是才正常运行。

  • ??的用法

4. Enumerations - 枚举

enum 名称 {
    case 枚举名1
    case 枚举名2(附加值: 类型)
    case 枚举名3
}

5. Structs- 结构体

好像没啥好写的

6. Classes - 类

  • 继承

Values vs References 值与引用

When you copy a struct, the whole thing is duplicated, including all its values. This means that changing one copy of a struct doesn’t change the other copies – they are all individual. With classes, each copy of an object points at the same original object, so if you change one they all change. Swift calls structs “value types” because they just point at a value, and classes “reference types” because objects are just shared references to the real value.

This is an important difference, and it means the choice between structs and classes is an important one:

  • If you want to have one shared state that gets passed around and modified in place, you’re looking for classes. You can pass them into functions or store them in arrays, modify them in there, and have that change reflected in the rest of your program.
  • If you want to avoid shared state where one copy can’t affect all the others, you’re looking for structs. You can pass them into functions or store them in arrays, modify them in there, and they won’t change wherever else they are referenced.
发布了77 篇原创文章 · 获赞 16 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/hh680821/article/details/105187148