- iOS Programming Cookbook
- Hossam Ghareeb
- 111字
- 2021-07-09 18:29:46
How to do it...
Here, we will create the stack data structure with/without generics:
- Create a new playground named Generics.
- Let's create the data structure stack with type Int:
class StackInt{ var elements = [Int]() func push(element:Int) { self.elements.append(element) } func pop() ->Int { return self.elements.removeLast() } func isEmpty()->Bool { returnself.elements.isEmpty } } var stack1 = StackInt() stack1.push(5) // [5] stack1.push(10) //[5,10] stack1.push(20) // [5,10,20] stack1.pop() // 20
- Let's see the same created stack but with a generics fashion:
class Stack <T>{ var elements = [T]() func push(element:T) { self.elements.append(element) } func pop()->T{ return self.elements.removeLast() } } var stackOfStrings = Stack<String>() stackOfStrings.push("str1") stackOfStrings.push("str2") stackOfStrings.pop() var stackOfInt = Stack<Int>() stackOfInt.push(4) stackOfInt.push(7) stackOfInt.pop()
推薦閱讀
- Containerization with LXC
- BPEL and Java Cookbook
- 蘋果電腦玩全攻略 OS X 10.8 Mountain Lion
- RESS Essentials
- STM32庫開發(fā)實(shí)戰(zhàn)指南:基于STM32F4
- INSTANT Migration from Windows Server 2008 and 2008 R2 to 2012 How-to
- 操作系統(tǒng)分析
- Cassandra 3.x High Availability(Second Edition)
- Linux軟件管理平臺(tái)設(shè)計(jì)與實(shí)現(xiàn)
- Hadoop Real-World Solutions Cookbook
- Learn OpenShift
- Windows網(wǎng)絡(luò)編程(第2版)
- Windows7系統(tǒng)維護(hù)百寶箱
- Microsoft Azure Administrator:Exam Guide AZ-103
- Windows PE權(quán)威指南