本文是學習《The Swift Programming Language》整理的相關隨筆,基本的語法不作介紹,主要介紹Swift中的一些特性或者與OC差異點。
系列文章:
- Swift4 基礎部分:The Basics
- Swift4 基礎部分:Basic Operators
- Swift4 基礎部分:Strings and Characters
- Swift4 基礎部分:Collection Types
- Swift4 基礎部分:Control Flow
- Swift4 基礎部分:Functions
- Swift4 基礎部分:Closures
- Swift4 基礎部分: Enumerations
- Swift4 基礎部分: Classes and Structures
- Swift4 基礎部分: Properties
- Swift4 基礎部分: Methods
- Swift4 基礎部分: Subscripts
- Swift4 基礎部分: Inheritance
- Swift4 基礎部分: Initialization
- Swift4 基礎部分: Deinitialization
- Swift4 基礎部分: Automatic Reference Counting(自動引用計數)
- Swift4 基礎部分: Optional Chaining(可選鏈)
- Swift4 基礎部分: Error Handling(錯誤處理)
- Swift4 基礎部分: Type Casting(類型轉換)
- Swift4 基礎部分: Nested Types(嵌套類型)
Extensions add new functionality to an existing class,
structure, enumeration, or protocol type. This includes the
ability to extend types for which you do not have access to
the original source code (known as retroactive modeling).
Extensions are similar to categories in Objective-C. (Unlike
Objective-C categories, Swift extensions do not have names.)
Extensions in Swift can:
Add computed instance properties and computed type properties
Define instance methods and type methods
Provide new initializers
Define subscripts
Define and use new nested types
Make an existing type conform to a protocol
- Swift中的擴展與OC中的類別很相似,闊以用來擴展方法,屬性,同時還可以提供新的構造器,定義下標,定義與使用新的嵌套類型,使這個擴展實現某個協議。
計算屬性(Computed Properties)
Extensions can add computed instance properties and computed
type properties to existing types.
- 擴展可以添加計算屬性到已有類中。
例子:
extension Double {
var km:Double {
return self * 1000.0;
}
var m: Double {
return self;
}
var cm: Double {
return self / 100.0;
}
var mm: Double {
return self / 1_000.0;
}
var ft: Double {
return self / 3.28084;
}
}
let oneInch = 25.4.mm;
print("One inch is \(oneInch) meters");
let threeFeet = 3.ft;
print("Three feet is \(threeFeet) meters");
執行結果:
One inch is 0.0254 meters
Three feet is 0.914399970739201 meters
構造器(Initializers)
Extensions can add new initializers to existing types.
例子:擴展Rect通過center,size計算Rect的值
struct Size {
var width = 0.0,height = 0.0;
}
struct Point {
var x = 0.0, y = 0.0;
}
struct Rect {
var origin = Point();
var size = Size();
}
// MARK: - 添加一個擴展通過center,size計算rect
extension Rect {
init(center:Point,size:Size) {
let originX = center.x - (size.width / 2);
let originY = center.y - (size.height / 2);
self.init(origin: Point(x: originX, y: originY), size: size);
}
}
let centerRect = Rect(center: Point(x: 4.0, y: 4.0),
size: Size(width: 3.0, height: 3.0));
print(centerRect);
方法(Methods)
Extensions can add new instance methods and type methods to existing types.
- 擴展的方式可以添加新的實例方法和類方法到該類型。
例子:
extension Int {
func repetitions(task: () -> Void){
for index in 0..<self{
task();
}
}
func toBinary() -> String {
return String(self,radix:2);
}
// 必須用mutating修飾才能修改自身
mutating func square() {
self = self * self
}
}
3.repetitions(task:{
print("Hello");
})
print(3.toBinary());
var someInt:Int = 3;
someInt.square();
print(someInt);
執行結果:
Hello
Hello
Hello
11
9
定義下標(Subscripts)
Extensions can add new subscripts to an existing type.
例子:
extension Int {
subscript(digitIndex:Int) -> Int{
var decimalBase:Int = 1;
for index in 0..<digitIndex{
decimalBase *= 10;
}
return (self / decimalBase) % 10;
}
}
print(123456[2]);
print(123456[3]);
print(123456[4]);
執行結果:
4
3
2
嵌套類型(Nested Types)
Extensions can add new nested types to existing classes, structures, and enumerations
例子:
extension Int {
enum Kind {
case negative,zero,positive;
}
var kind:Kind{
switch self{
case 0:
return .zero;
case let x where x > 0:
return .positive;
default:
return .negative;
}
}
}
print(12345.kind);
print(0.kind);
print((-12345).kind);
執行結果:
positive
zero
negative