Swift4 基础部分: Extensions(扩展)
程序员文章站
2024-02-20 15:04:40
...
本文是学习《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