本文是學(xué)習(xí)《The Swift Programming Language》整理的相關(guān)隨筆,基本的語(yǔ)法不作介紹,主要介紹Swift中的一些特性或者與OC差異點(diǎn)。
系列文章:
- Swift4 基礎(chǔ)部分:The Basics
- Swift4 基礎(chǔ)部分:Basic Operators
- Swift4 基礎(chǔ)部分:Strings and Characters
- Swift4 基礎(chǔ)部分:Collection Types
- Swift4 基礎(chǔ)部分:Control Flow
- Swift4 基礎(chǔ)部分:Functions
- Swift4 基礎(chǔ)部分:Closures
- Swift4 基礎(chǔ)部分: Enumerations
- Swift4 基礎(chǔ)部分: Classes and Structures
- Swift4 基礎(chǔ)部分: Properties
- Swift4 基礎(chǔ)部分: Methods
- Swift4 基礎(chǔ)部分: Subscripts
- Swift4 基礎(chǔ)部分: Inheritance
- Swift4 基礎(chǔ)部分: Initialization
- Swift4 基礎(chǔ)部分: Deinitialization
- Swift4 基礎(chǔ)部分: Automatic Reference Counting(自動(dòng)引用計(jì)數(shù))
- Swift4 基礎(chǔ)部分: Optional Chaining(可選鏈)
- Swift4 基礎(chǔ)部分: Error Handling(錯(cuò)誤處理)
- Swift4 基礎(chǔ)部分: Type Casting(類型轉(zhuǎn)換)
- Swift4 基礎(chǔ)部分: 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中的擴(kuò)展與OC中的類別很相似,闊以用來(lái)擴(kuò)展方法,屬性,同時(shí)還可以提供新的構(gòu)造器,定義下標(biāo),定義與使用新的嵌套類型,使這個(gè)擴(kuò)展實(shí)現(xiàn)某個(gè)協(xié)議。
計(jì)算屬性(Computed Properties)
Extensions can add computed instance properties and computed
type properties to existing types.
- 擴(kuò)展可以添加計(jì)算屬性到已有類中。
例子:
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");
執(zhí)行結(jié)果:
One inch is 0.0254 meters
Three feet is 0.914399970739201 meters
構(gòu)造器(Initializers)
Extensions can add new initializers to existing types.
例子:擴(kuò)展Rect通過(guò)center,size計(jì)算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: - 添加一個(gè)擴(kuò)展通過(guò)center,size計(jì)算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.
- 擴(kuò)展的方式可以添加新的實(shí)例方法和類方法到該類型。
例子:
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);
執(zhí)行結(jié)果:
Hello
Hello
Hello
11
9
定義下標(biāo)(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]);
執(zhí)行結(jié)果:
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);
執(zhí)行結(jié)果:
positive
zero
negative