Listing 22.4 gives compiler warning (Swift 3.1)

The line


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) {
    mutating func pop() -> Element? {
        guard !items.isEmpty else {
            return nil
        return items.removeLast()

var intStack = Stack<Int>()
 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
// print(intStack.pop())
print(intStack.pop() as Any)
print(intStack.pop() as Any)
print(intStack.pop() as Any)


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.

Agreed - but in their defense - the compiler does tell you what to do. Wonder if this was a Swift 2 - 3 issue?