The line
print(intStack.pop())
generates a Swift Compiler Warning (under Swift 3.1).
Following is the full listing with comments about the fix.
import Cocoa
struct Stack<Element> {
var items = [Element]()
mutating func push(_ newItem: Element) {
items.append(newItem)
}
mutating func pop() -> Element? {
guard !items.isEmpty else {
return nil
}
return items.removeLast()
}
}
var intStack = Stack<Int>()
intStack.push(1)
intStack.push(2)
/*
print(intStack.pop())
the above line (from Listing 22.4 in the book) generates a Swift Compiler Warning in Swift v. 3.1
a fix is to add "as Any" as below
see https://stackoverflow.com/questions/40254820/accessing-indices-of-swift-arrays-safely-ext-return-not-an-optional#40254881
*/
// print(intStack.pop())
print(intStack.pop() as Any)
print(intStack.pop() as Any)
print(intStack.pop() as Any)
Cheers,
Jim
P.S. If the authors are listening here, may I suggest that you run all the code listings in the book under the new Swift releases as they come out? Then issue errata information about the issues the code listings generate under the newer versions of Swift. Doing so would sure help a rank beginner like me! A new learner should be able to type in the code from the book or errata information and not get compiler errors or warnings.