一、数据类型
Swift 包含了 C 和 Objective-C 上所有基础数据类型,Int 表示整型值、Double 和 Float 表示浮点型值、Bool 是布尔型值、String 是文本型数据。Swift 还提供了三个基本的集合类型:Array、Set 和 Dictionary。
Swift 还提供了 OC 中没有的高阶数据类型比如元组 Tuple。元组可以让你创建或者传递一组数据,元组的数据类型不必一致。
Swift 还增加了可选(Optional)类型,用于处理值缺失的情况。可选表示一个变量可以有一个值或者没有值。
Swift 是一种类型安全的语言,Swift 可以让你清楚地知道值的类型。如果你的代码期望得到一个 String,类型安全会阻止你不小心传入一个 Int。同样的,如果你的代码期望得到一个 String,类型安全会阻止意外传入一个可选的 String。类型安全帮助在开发阶段尽早发现错误。
二、常量和变量
常量和变量把一个名字和一个指定类型的值关联起来。常量的值一旦设定就不能改变,而变量的值可以随意更改。
1.声明常量和变量
常量和变量必须在使用前声明,用 let 声明常量,用 var 声明变量。
2.类型标注
当声明常量或者变量的时候可以加上类型标注(type annotation),说明常量或者变量中要存出的值得类型。
var a:Int=1
可以在一行中定义多个同样类型的变量,用逗号分隔,并在最后一个变量名之后添加类型标注。
var a,b,c:String
三、元组
元组(tuples)把多个值组合成一个复合值。元组内的值可以是任意类型,并不要求是相同类型。
(100,"cjj") 是一个 (Int,String) 类型的元组。
可以把任意顺序的类型组合成一个元组,这个元组可以包含所有类型。
可以将一个元组的内容分解(decompose)成单独的常量和变量,然后可以正常使用了。
var a=(100,"cjj")
var (c,d)=a
四、可选类型
OC 中没有 Optional 类型,OC 中所有对象变量都可以为 nil,因为 nil 是无类型的指针。在 OC 中字典、数组、集合都不能放入 nil,nil 只能用在 OC 对象上面,变量在一定程度上来讲便利性较差,但在 Swift 中却不同。Swift 中 nil 和 OC 中的 nil 是有很大区别的。在 OC 中 nil 是指向一个不存在的对象的指针,但是在 Swift 中,nil 不是指针,只是值缺失的特殊类型,任何类型可选项都可以设置为 nil。所以在 Swift 中,可以用可选项值为 nil,来表达变量的值缺失,增加了一定的便利性。
Swift 中我们在变量类型后面添加 ? 来表示一个可选项,例如:
var name: String? = nil
1.实现
Optional 其实是一个枚举类型,我们查看标准库中代码可以看到
@frozen public enum Optional<Wrapped> : ExpressibleByNilLiteral {
/// The absence of a value.
///
/// In code, the absence of a value is typically written using the `nil`
/// literal rather than the explicit `.none` enumeration case.
case none
/// The presence of a value, stored as `Wrapped`.
case some(Wrapped)
/// Creates an instance that stores the given value.
public init(_ some: Wrapped)
这个枚举有两个值,代码 Optional 的两层意思
-
none代表变量没有值,即为nil -
some代表变量有值,值为some,some包装了实际了值
那 Optional是如果得到实际的值呢,还是来看标准库中的代码
/// The wrapped value of this instance, unwrapped without checking whether
/// the instance is `nil`.
///
/// The `unsafelyUnwrapped` property provides the same value as the forced
/// unwrap operator (postfix `!`). However, in optimized builds (`-O`), no
/// check is performed to ensure that the current instance actually has a
/// value. Accessing this property in the case of a `nil` value is a serious
/// programming error and could lead to undefined behavior or a runtime
/// error.
///
/// In debug builds (`-Onone`), the `unsafelyUnwrapped` property has the same
/// behavior as using the postfix `!` operator and triggers a runtime error
/// if the instance is `nil`.
///
/// The `unsafelyUnwrapped` property is recommended over calling the
/// `unsafeBitCast(_:)` function because the property is more restrictive
/// and because accessing the property still performs checking in debug
/// builds.
///
/// - Warning: This property trades safety for performance. Use
/// `unsafelyUnwrapped` only when you are confident that this instance
/// will never be equal to `nil` and only after you've tried using the
/// postfix `!` operator.
@inlinable public var unsafelyUnwrapped: Wrapped { get }
它是一个定义的 get 方法,Optionl 通过 unsafelyUnwrapped 来获取实际的值,例如
var ddb: String? = "哈哈哈"
let ddbCount = ddb.unsafelyUnwrapped.count
这样就得到了变量的实际值。
2.使用
1.实现一个 Optional
let ddb: Optional<String> = "哈哈哈"
// var ddb: String? = "哈哈哈"
我们这样实现的可选项,实际上和注释部分的类型后面加 ? 实现的是完全一样的。
3.可选项的解包
可选项是不能直接使用的,需要解包后才能使用,基本上有一下解包方式
(1)!强制解包
let count = ddb!.count
在强制解包前,你如果不知道它是否为 nil,那你需要先对它进行非 nil的判断保护,否则强制解包一旦失败,程序会报错,如下代码:
if ddb != nil {
let count = ddb!.count
print(count)
}
这样即使我们使用了强制解包,但它的运行依然是安全的
(2)if 判断展开
if ddb != nil {
let count = ddb?.count
print(count ?? 0)
}
这里我们使用 a ?? b 合并空值运算符的方式来解包,如果有值,则为 count,如果为 nil,则默认 0
使用合并控制运算符有两个条件:
1.表达式 a 必须是可选类型
2.表达式 b 必须和 a 的类型相同
(3)使用可选项绑定的方式
if let ddbStr = ddb {
let count = ddbStr.count
print(count)
}
使用可选项绑定来判断可选项是否有值,如果有就赋值给临时变量。同一个 if 语句可以有多个可选项绑定,用,分开即可
小结
Optional是”不存在“或“空”概念的加强版本。而 nil 则是“不存在”的基础版本
在 Swift 中引入 Optional 的目的,就是将"不存在"这个概念绑定到具体的类型上。optional.nil 指向的是值的“不存在”,同时表示:如有值只能是 optional.some<T> 中的 T 类型,将所有类型的值空间进行了 nil 的扩展。










网友评论