Today in practice design patterns when there is no sudden discovery Swift stack - Stack class is。Since I was shining Java version of the design patterns to learn,So I had no choice but to implement a look at the。
But speaking,In Apple's official manual, "generic" chapter really have a stack of chestnuts,It is the structure implemented。This is actually in line with the Swift's style - after all,, Stack did not need to achieve with the class - in the structure of the Swift is。
Here we briefly review what is the stack - in fact, it may have different definitions in different levels,We refer here to the definition of Wikipedia:
Stack(English:stack),It can also be directly calledStack。For TaiwanStacking,existcomputer sciencein,It is a special form of serial data structures,It is special because only allow one end of the linked list or array (called the stack top index,English:top) Were added data (English:push) And output data (English:pop) Operation。In addition it can also be stacked with one-dimensionalArrayorSerial LinkForm to complete。Stacking another mode of operation is called a relativeQueue。
Since the stack data structure may only be performed at one end,Thus according to the LIFO (LIFO, Last In First Out) principle of operation。
Stack data structure uses two basic operations:Push (push) and pop (pop):
- Push:Put data stack to the top (in the form of an array or tandem),Plus a stack top top Indicators。
- pop up:The top data output (return),Save a data stack to the top。
Generic version
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
struct StackG<Element> { private var items = [Element]() mutating func push(item:Element) { items.append(item) } mutating func pop() ->Element? { return items.removeLast() } func empty() -> Bool { return items.isEmpty } func peek() ->Element? { return items.last } } |
Here we use a generic array as a stack of storage,Then is a simple push and pop,Other,I also added two for detecting whether the stack is empty and does not remove the top to see only way,In that case,On and in Java Stack class almost。
To use the words to declare a generic manner in accordance with: where GVersionStack = StackG<SomeClass>()
Because here it is the structure we use,So to declare a variable rather than a constant。Also it is therefore,Our part of the structure in vivo data have marked a modified approach mutating Keyword。
Here we look at a use of chestnuts:
1 2 3 4 5 6 |
class SomeClass { var classNumber = 0 init (number:Int) { classNumber = number } } |
I readily wrote a class,While we use the stack to store the instance of this class (reference)。
1 2 3 4 5 6 7 8 9 10 |
var GVersionStack = StackG<SomeClass>() let a = SomeClass(number: 1) let b = SomeClass(number: 2) let c = SomeClass(number: 3) GVersionStack.push(a) GVersionStack.push(c) GVersionStack.push(b) print(GVersionStack.peek()!.classNumber) |
I declared three different instances,Give them a different serial number to track,Then pushed onto the stack in。Because it is generic,So after stack of data must be SomeClass Type (or subclass),Otherwise you can not enter。This is also consistent with the type-safe language style Swift。
Class version
but,I still want to get it on like Java Stack class as,How to do it? I put the stack changed to change:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Stack { private var items = [Any]() func push(item:Any) { items.append(item) } func pop() ->Any? { return items.removeLast() } func empty() -> Bool { return items.isEmpty } func peek() ->Any? { return items.last } } |
This time I used a class instead of a structure to achieve,So get rid of the generic statement when more convenient,It can also be declared as constant - because it saved a reference to the class。
1 |
let stack = Stack() |
Here we declare an array of type became Any ,So that it can refer to any type of,Including classes,Swift also included in the basic types。
note
Because Swift in the basic types is through packaging structure,And they are not inherited from AnyObject ,If so write as OC,It can not be put into basic types! and,Swift does not have a default inherit the base class habits,So we use Any rather than AnyObject 。
but,In this way there will be no type,No matter what you put into it,Swift does not help you check,Because they have become Any ,Then we will manually check type。All right,Chestnut still on top of that SomeClass ,This time we use a new stack to try:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
let stack = Stack() let a = SomeClass(number: 1) let b = SomeClass(number: 2) let c = SomeClass(number: 3) stack.push(1) //Int stack.push(a) //SomeClass stack.push("abcd") //String while(!stack.empty()) { let tmp = stack.peek() as? SomeClass //Type cast if let a = tmp { print(tmp!.classNumber) } stack.pop() } |
This time I not only pushed the SomeClass ,Also pressed into the string and shaping the digital,Everything is so perfect。
Then,I turn pop the stack of data,But I have to convert them before using them in formats,Otherwise, you can not use - because they do not know the Swift already。
Further reading
At last,I have written these two stack foul to spread their shameless Github on,For you to download and use reference;
About generic content,Self-referenceHere;
About StackWikipedia。
Original article written by LogStudio:R0uter's Blog » Swift Lane Stack implementation
Reproduced Please keep the source and description link:https://www.logcg.com/archives/1505.html
Comments