import Foundation
struct Point {
var x: Int
var y: Int
}
struct Size {
var width: Int
var height: Int
}
class Rect {
var point: Point
var size: Size
init(point: Point, size: Size) {
self.point = point
self.size = size
}
}
// 只能扩展五种东西
// 1. 方法
// 2. 计算型属性
// 3. 便利的构造函数
// 4. 嵌套的枚举类型
// 5. 下标
// 不允许扩展成员变量和指定的构造函数
extension Rect {
var center: Point {
return Point(x: point.x + size.width / 2, y: point.y + size.height / 2)
}
convenience init(center: Point, size: Size) {
let originX = center.x - size.width / 2
let originY = center.y - size.height / 2
self.init(point: Point(x: originX, y: originY), size: size)
}
func printHello() {
print("Hello!")
}
}
let rect = Rect(center: Point(x: 2, y: 2), size: Size(width: 4, height: 4))
rect.point
rect.printHello()
2. 扩展嵌套的枚举类型和下标
import Foundation
struct Point {
var x: Int
var y: Int
}
struct Size {
var width: Int
var height: Int
}
class Rect {
var point: Point
var size: Size
init(point: Point, size: Size) {
self.point = point
self.size = size
}
}
extension Rect {
// 扩展嵌套的枚举类型
enum Vertex: Int {
case LeftTop // 默认关联值为0
case RightTop
case RightBottom
case LeftBottom
}
func pointAtVertex(v: Vertex) -> Point {
switch v {
case .LeftTop:
return point
case .RightTop:
return Point(x: point.x + size.width, y: point.y)
case .RightBottom:
return Point(x: point.x + size.width, y: point.y + size.height)
case .LeftBottom:
return Point(x: point.x, y: point.y + size.height)
}
}
// 扩展下标
subscript(index: Int) -> Point {
assert(index >= 0 && index <= 3)
return pointAtVertex(v: Vertex(rawValue: index)!) // 回顾知识点,通过rawValue得到的枚举类型都是可选型
}
}
let rect = Rect(point: Point(x: 0, y: 0), size: Size(width: 10, height: 10))
rect[2] // 10, 10
rect.pointAtVertex(v: .RightBottom) // 10, 10
3. 扩展系统类库,这里以Int为?
import UIKit
extension Int {
var suqare: Int {
return self * self
}
var cube: Int {
return self * self * self
}
func inRange(closedLeft left: Int, openedRight right: Int) -> Bool {
return self >= left && self < right
}
// 函数式编程思想,重复执行这么多次
func repititions(task: () -> ()) {
for _ in 0 ..< self {
task()
}
}
// 自定义一个扩展的函数,用于指定的循环,并打印其平方
func myStride(to: Int, by: Int, task: (_ num: Int) -> ()) {
for i in stride(from: self, to: to, by: by) {
task(i)
}
}
subscript(index: Int) -> Int { // 通过下标找出一个数的第几位是哈
var len = 0
var temp1 = self
var temp2 = temp1
while temp1 != 0 {
temp1 /= 10
len += 1
}
let CircleTime = len - index // 循环次数
var tail: Int = 0
for _ in 0 ..< CircleTime {
tail = temp2 % 10
temp2 /= 10
}
return tail
}
func toBinary() -> Int {
var temp = self
var sum = 0
var len = 0
var tail: Int = 0
while temp != 0 {
len += 1
tail = temp % 2
sum += tail * Int(pow(10.0, Double(len - 1))) // 这里强烈吐槽pow函数!!!就没有Int吗,非要强转!!!
temp /= 2
}
return sum
}
func isPrime() -> Bool {
assert(self >= 0)
switch self {
case 0, 1:
return false
case 2, 3:
return true
default:
for i in 2 ... Int(sqrt(Double(self))) {
if self % i == 0 {
return false
}
}
return true
}
}
}
let num = 2
num.cube
num.suqare
num.inRange(closedLeft: 0, openedRight: 8)
num.repititions {
print("I love Bytedance")
}
num.myStride(to: 10, by: 2) { i in
print(i.suqare) // 从2的平方打印到8的平方
}
let num2 = 434236
num2[0] // 4
num2[1] // 3
num2[2] // 4
num2[3] // 2
16.toBinary() // 10000
18.toBinary() // 10010
1.toBinary() // 1
1024.toBinary() // 10_000_000_000
1.isPrime() // false
2.isPrime() // true
3.isPrime() // true
4.isPrime() // false
17.isPrime() // true
49.isPrime() // false
101.isPrime() // true
1. 扩展初体验 Ps:只能扩展五种东西 1. 方法 2. 计算型属性 3. 便利的构造函数 4. 嵌套的枚举类型 5. 下标 不允许扩展成员变量和指定的构造函数import Foundationstruct Point { var x: Int var y: Int}struct Size { var width: Int ...