Swift 类型

这篇文章主要讲 Swift 中的特殊类型。

预览

Swift 中包含两种类型: 命名类型(named type)混合类型(compound type)命名类型是一种在定义的时候给定一个特殊名称的类型。命名类型包含类、结构、枚举、协议。例如用户定义的一个以 MyClass 命名的类的一个实例的类型是 MyClass。 除用户定义的命名类型外, 在 Swift 标准库中定了许多公共使用的命名类型,包括数组、字典、可选值

在其他语言中通常被认为是基本的数据类型 -- 例如呈现数字、字符、字符串实际上是命名类型,在 Swift 标准库中他们经常使用结构体定义和实现。因为他们是命名类型所以你能使用扩展申明去扩展他们的行为以满足你程序的需求。

混合类型是一种不需要名字的类型,被定义在 Swift 自己当中的语言。Swift 中有两种混合类型:** 函数类型元组类型混合类型 能包含命名类型 和其它的混合类型**

你可以把 命名类型 混合类型放在一对小括号中,然而添加一对小括号不会照成任何影响。例如(Int) 等价于 Int。例如元组包含两个元素 (Int, (Int, Int)), 第一个是命名类型 Int另外一个是混合类型(Int, Int)。

类型标注(Type Annotation)

类型标注明确的指定变量或者表达式类型类型标注以冒号开始以类型结尾,例子如下:

var someTuple: (Double, Double) = (3.14159, 2.71828)
func someFunction(a: Int){/* ... */}

例子一中,表达式 someTuple 指定元组类型 (Double, Double) ,例子二中,函数 someFunction 参数 a 指定为类型 Int 。
类型标注中,在类型之前包含类型属性的可选列表。

GRAMMAR OF A TYPE ANNOTATION
type-annotation → : attributes opt inoutopt type

类型标识(Type Identifier)

类型标识指向命名类型命名类型混合类型的别名。两种情况下 类型标识和类型名称不一致,第一种情况是指向命名类型混合类型的别名,第二种情况是命名类型在其他模块或者嵌套在其它类型中。
示例一, origin 的类型标识为 Point,但类型标注指向 (Double, Double)。

typealias Point = (Double, Double)
let origin: Point = (0, 0)

示例二, someValue 的类型名为 MyType, 类型标识为 ExampleModule.MyType

var someValue: ExampleModule.MyType

可选类型 (Optional Type)

Swift 语言定义 ? 后缀作为 Optional<Wrapped> 命名类型的语法糖, 这定义在 Swift 的标准库中。 换句话说以下两种申明是等价的。

var optionalInteger: Int?
var optionalInteger: Optional<Int>

使用 ! 去强制解包可选类型,如果值为空则会引发运行时错误。

协议组合类型(Protocol Composition Type)

协议组合类型 定义了一种可以由多个协议组合成的或者一个类伴随着多个协议组合成的新类型。协议组合类型仅仅能在指定类型标注泛型参数申明或者泛型 where 申明 中使用。
协议组合类型格式如下:

Protocol 1 & Protocol 2

CombineAB 为协议 AB组合协议类型, 使用关键字 typealias 标识协议组合类型

protocol A {
    func a() throws -> String
}

protocol B {
    func b() throws -> String
}

typealias CombineAB = A & B

ClassAndProtocolCombine组合协议类型 CombineAB 和 BaseClass组合协议类型,当其中存在时有且仅有只能一个存在。

class BaseClass {
    class func printClass() {
        print("BaseClass")
    }
}

typealias ClassAndProtocolCombine = CombineAB & BaseClass

元类型(MetaType Type)

元类型指向任何类型的类型,包括类类型、结构类型、枚举类型、协议类型。类、结构、枚举类型的元类型是他们的名字加上.Type。协议类型是协议名加上.Protocol。例如 SomeClass 的元类型是 SomeClass.Type, SomeProtocol的元类型是 SomeProtocol.Protocol。

class SomeBaseClass {
    class func printClassName() {
        print("SomeBaseClass")
    }
}

class SomeSubClass: SomeBaseClass {
    override class func printClassName() {
        print("SomeSubClass")
    }
}

let someInstance: SomeBaseClass = SomeSubClass()
let  type(of: someInstance). printClassName()
// Output 为 SomeSubClass

另一个例子

class AnotherSubClass: SomeBaseClass {
    let string: String
    required init(string: String) {
        self.string = string
    }
    override class func printClassName() {
        print("AnotherSubClass")
    }
}
let metatype: AnotherSubClass.Type = AnotherSubClass.self
metatype.init(string: "some string")

任何类型 (Any Type)

任何类型可以是其它的所有类型, ** Any** 能是其它具体类型的实例,包含的类型有:

  • 类,结构体、枚举
  • 元类型例如, Int.self
  • 元素为任何类型的组件元组
  • 函数或者闭包类型
    例子如下

let mixed: [Any] = ["one", 2, true, (4, 5.3), { () -> Int in return 6 }]

值得注意的是 Any 类型使用的时候需要使用 as 关键字做类型转换, 例如:

let string = mixed.first as? String {
print("The first element of mixed is \(string)")
}

Self Type

示例代码:

class Superclass {
    func f() -> Self { return self }
}
let x = Superclass()
print(type(of: x.f()))
// Prints "Superclass"

class Subclass: Superclass { }
let y = Subclass()
print(type(of: y.f()))
// Prints "Subclass"

let z: Superclass = Subclass()
print(type(of: z.f()))
// Prints "Subclass"
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容