-
Notifications
You must be signed in to change notification settings - Fork 48
/
Browser.swift
67 lines (59 loc) · 1.49 KB
/
Browser.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// Browser.swift
// Stack
//
// Created by ggl on 2019/4/9.
// Copyright © 2019年 ggl. All rights reserved.
//
import Foundation
class Browser {
var curStack: ArrayStack<String>
var forwardStack: ArrayStack<String>
var curPage: String {
if let page = curStack.pop() {
curStack.push(page)
return "当前页面为:\(page)"
} else {
return "目前没有打开任何页面"
}
}
init() {
curStack = ArrayStack<String>()
forwardStack = ArrayStack<String>()
}
/// 打开新页面
///
/// - Parameter page: 新页面
func openNewPage(_ page: String) {
print("打开新页面:\(page)")
curStack.push(page)
}
/// 页面前进
///
/// - Returns: 前进是否成功
@discardableResult
func goForward() -> Bool {
if let page = forwardStack.pop() {
print("前进到页面:\(page)")
curStack.push(page)
return true
} else {
print("暂无页面可前进")
return false
}
}
/// 页面后退
///
/// - Returns: 后退是否成功
@discardableResult
func goBackward() -> Bool {
if curStack.count <= 1 {
print("暂无页面可后退")
return false
}
let page = curStack.pop()!
forwardStack.push(page)
print("页面后退,\(curPage)")
return true
}
}