let status = getHaterStatus(weather: "rainy")?.someOptionalValue?.someOtherOptionalValue?...
The nil coalescing operator:??
??:零合并操作符,
使用:
let status = getHaterStatus(weather: "rainy") ?? "unknown"
// 当 ?? 左边表达式为空时,则使用右边的表达式
print("The Hater Status: \(status)")
枚举
// 定义
enum WeatherType {
case sun
case cloud
case rain
case wind
case snow
}
func getHaterStatus(weather: WeatherType) -> String? {
if weather == .sun {
return nil
} else {
return "Hate"
}
}
// 使用
getHaterStatus(weather: .cloud)
枚举附加值
// 定义
enum WeatherType {
case sun
case cloud
case rain
case wind(speed: Int)
case snow
}
func getHaterStatus(weather: WeatherType) -> String? {
switch weather {
case .sun:
return nil
case .wind(let speed) where speed < 10:
return "meh"
case .cloud, .wind:
return "dislike"
case .rain, .snow:
return "hate"
}
}
// 使用
getHaterStatus(weather: WeatherType.wind(speed: 5))
Struct:结构
// 定义
struct Person {
var clothes: String
var shoes: String
}
// 使用
let taylor = Person(clothes: "T-shirts", shoes: "sneakers")
let other = Person(clothes: "short skirts", shoes: "high heels")
print(taylor.clothes)// 输出:T-shirts
print(other.shoes)// 输出:high heels
// 修改结构定义
struct Person {
var clothes: String
var shoes: String
func describe() {
print("I like wearing \(clothes) with \(shoes)")
}
}
let taylor = Person(clothes: "T-shirts", shoes: "sneakers")
// 调用结构的函数
taylor.describe()
Class:类
Class和Struct有很多相同之处,但也有很多不同之处:
网站原文:
You don't get an automatic memberwise initializer for your classes; you need to write your own.
You can define a class as being based off another class, adding any new things you want.
When you create an instance of a class it’s called an object. If you copy that object, both copies point at the same data by default – change one, and the copy changes too.
#### 继承
##### 重写
```swift
class Singer {
var name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
func sing() {
print("La la la la")
}
}
class CountrySinger: Singer {
// 覆盖父类中的同名方法
override func sing() {
print("Trucks, guitars, and liquor")
}
}
var taylor = CountrySinger(name: "Taylor", age: 25)
taylor.sing()// 输出:Trucks, guitars, and liquor
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.
If I were to summarize this key difference between structs and classes, I'd say this: classes offer more flexibility, whereas structs offer more safety. As a basic rule, you should always use structs until you have a specific reason to use classes.
iOS学习笔记(二)基础篇(中)
环境版本:
函数
基本定义:
省略外部参数:
内外使用不同的参数:
Optionals(可选项?)
类似于
Java
中的Optional
类 在函数有返回值,且返回值可能为空时就可以使用!
:表示确定变量有值,所以强行打开,如果打开一个值为nil
,则程序会崩溃(没实际测试过)Optional Chaining
继续使用上一节的函数
我们可以将上述代码中最后两行稍加修改,代码如下:
注意表达式中的
?
,这就是optional chaning
:问号后面所有的内容只在问号前所有的内容都有值时才会运行。 可以使用多次?
The nil coalescing operator:??
??
:零合并操作符, 使用:枚举
枚举附加值
Struct:结构
复制struct
将
struct
变量赋值给另一个变量,并不是别名
,而是复制了原结构变量的值,之后的修改互不影响。结构中定义函数
Class:类
Class
和Struct
有很多相同之处,但也有很多不同之处: 网站原文:机翻(略作修改):
类的实例被称为对象。如果复制该对象,则默认情况下两个副本都指向相同的数据(别名) - 更改一个,副本也会更改。
类的定义
class Person { var clothes: String var shoes: String
}
新的属性
如何选择使用类还是结构
原文:
机翻: