一,元组(tuples):把多个值组合成一个复合值。元组内的值可以是任意类型,并不要求是相同类型
let http404Error = (404,"Not Found")
也可以将元组的内容分解成单独的常量和变量
let (statusCode,statusMessage) = http404Error
print("The status code is \(statusCode),and the status message is \(statusMessage)")
如果只需要一部分值,分解的时候可以把要忽略的部分用下划线( _ ) 标记
let (justTheStatusCode, _) = http404Error
定义元组的时候,可以给元组里的单个元素命名,然后通过元组名.元素名来获取值
let http200Status = (statusCode: 200, decription:"OK")
print("code: \(http200Status.statusCode) descrip:\(http200Status.decription)")
二,可选类型
使用它来处理值可能缺失的情况,有值等于x, 或没有值(nil)
注意: 在OC中,nil是一个指向不存在对象的指针,在swift中,nil不是指针,它是一个确定的值,用来表示值缺失,任何类型的可选状态都可以被设置为nil,不只是对象类型
Int? 等价于 Optional<Int> 官方文档:https://developer.apple.com/documentation/swift/optional
以下两种表示方法都是一样的意思
let shortForm: Int? = Int("42")
let longForm: Optional<Int> = Int("42")
var optionalName: String? = "Eileen"
var greeting = "Hello"
if let name = optionalName {
greeting = "Hello, \(name)"
}
print(greeting) //输出Hello, Eileen
上面的if let 语法意思就是 当optionalName不等于nil时,就赋值给name,即等价于
if optionalName != nil {
var name: String? = optionalName //如果不将name定义为可选类型,会报错
greeting = "Hello, \(name)"
}
print(greeting) //输出Hello, Optional("Eileen") 不解。。。
对于Optional("Eileen"),要想输出正确的值,就需要使用 if let 语句。它就是针对可选类型进行的拆包,当然还有别的方式,比如使用!强制拆包
Use ..< to make a range that omits its upper value, and use ...to make a range that includes both values.
三,方法(function)
(1),一般情况下,方法用它本身的参数名当做标签来传参,但是也可以在参数名前自定义标签,或者用来省略传参标签, 如下person前的 和 "at"
func greet(_ person: String, at day: String) -> String {
return "Hello \(person), today is \(day)."
}
greet("John", at: "Wednesday")
四,类
1), 定义变量的时候必须记得要赋值,要么是在声明的时候,要么是init初始化方法里, 如下name变量的赋值是在初始化方法里,也可以像numberOfSides在声明时就赋值
class NamedShape {
var numberOfSides: Int = 0
var name: String
init(name: String) {
self.name = name
}
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
2), 判断字符串变量是否等于某个值时直接用"=="
五,数据结构
- iOS里数据结构有3大类: 数组,字典,集合
- 在OC里 用 NSArray 和 NSMutableArray 表示数组的不可变和可变, 在Swift里,用let 和 var 来定义数组的不可变和可变。另外,NSArray里存放的数据可以是多种不同的类型,而swift里,一般只能存储同一种类型的数据
六,值类型和引用类型
1)在swift里,值类型有: enum, struct, string, array, dictionary等,引用类型有class,注意和OC的区别
2)在所有的值类型里,比较两实例是否相同用 ==,并且遵循 Equatable协议, 而引用类型,比较两实例是否相同用===(内存地址要相同)
相关链接:https://developer.apple.com/swift/blog/?id=10,
Consider, though, that Swift uses value types almost exclusively, which is mind-boggling when you consider that the situation in Objective-C is completely the reverse.
Swift里,值类型用的特别溜,特别多,而OC恰好相反,引用类型使用居多
3),在swift里,实例方法有3种: 类(class)的实例方法,结构体(struct)的实例方法,以及枚举类(enum)的实例方法。其中结构体和枚举属于值类型,默认情况下,值类型的属性是不可以在其实例方法里进行修改的。如果一定要修改,则需要在方法名func 前加上 mutating,表示可以修改值类型的实例以及所拥有的属性
另外,在创建值类型的实例时,如果你想通过该实例调用mutating 方法,则必须用var 声明并创建该实例
This behavior is due to structures being value types. When an instance of a value type is marked as a constant, so are all of its properties.
The same is not true for classes, which are reference types. If you assign an instance of a reference type to a constant, you can still change that instance’s variable properties.
举例如下
struct FixedLengthRange {
var firstValue: Int
let length: Int
}
//使用var声明
var rangeOfThreeItems = FixedLengthRange(firstValue: 0, length: 3)
// the range represents integer values 0, 1, and 2
rangeOfThreeItems.firstValue = 6
// the range now represents integer values 6, 7, and 8
let rangeOfFourItems = FixedLengthRange(firstValue: 0, length: 4)
// this range represents integer values 0, 1, 2, and 3
rangeOfFourItems.firstValue = 6
// 会报错, 即使 firstValue 是变量
4)写时复制COW copy-on-write
阅读链接:http://www.jianshu.com/p/229ce75f91de
5)枚举类型
定义为字符串类型的枚举,如果不特别指出case后的具体赋值,则编译器默认其值跟case 名保持一致
Swift does something special for enums with a String representation. If you don’t specify what the case is equal to, the compiler automatically makes the String the same as the name of the case.
例如:
enum ColorName: String {
case black, silver, gray, white, maroon, red, purple, fuchsia, green, lime, olive, yellow, navy, blue, teal, aqua
} //默认 black = "black".....
6)可失败构造器
如果一个类、结构体或枚举类型的对象,在构造过程中有可能失败,则为其定义一个可失败构造器。这里所指的“失败”是指,如给构造器传入无效的参数值,或缺少某种所需的外部资源,又或是不满足某种必要的条件等。其语法为在init关键字后面添加问号(init?)。
七,下标(subscript)
语法:同时结合了方法和计算属性的定义语法,如下
subscript(parameterList) -> ReturnType {
get {
// return someValue of ReturnType
}
set (newValue) {
// set someValue of ReturnType to newValue
}
}
网友评论