Type Stack<Element> does not conform to protocol 'Sequence'

Guys I am getting this error “Type Stack does not conform to protocol ‘Sequence’” Does anybody know what the issue is?

import Cocoa

struct StackGenerator<T>: IteratorProtocol {
    
    var stack: Stack<T>
    
    mutating func next() -> T? {
        return stack.pop()
    }
}

struct Stack<Element>: Sequence { /// ERROR Type Stack<Element> does not conform to protocol 'Sequence' 
    var items = [Element]()
    
    mutating func push(newItem: Element) {
        items.append(newItem)
    }
    mutating func pop() -> Element? {
        guard !items.isEmpty else {
            return nil
        }
        return items.removeLast()
    }
    
    func map<U>(f: (Element) -> U) -> Stack<U> {
        var mappedItems = [U]()
        for item in items {
            mappedItems.append(f(item))
        }
        return Stack<U>(items: mappedItems)
    }
    internal func generator() -> StackGenerator<Element> {
        return StackGenerator(stack: self)
    }
}

var intStack = Stack<Int>()
intStack.push(newItem: 1)
intStack.push(newItem: 2)
var doubledStack = intStack.map(f: {2 * $0})


print(intStack.pop())
print(intStack.pop())
print(intStack.pop())

print(doubledStack.pop())
print(doubledStack.pop())

var stringStack = Stack<String>()
stringStack.push(newItem: "This is a string")
stringStack.push(newItem: "another string")
print(stringStack.pop())

func myMap<T,U>(items: [T], f: (T) -> (U)) -> [U] {
    var result = [U]()
    for item in items {
        result.append(f(item))
    }
    return result
}

let strings = ["one", "two", "three"]
let stringLenghts = myMap(items: strings) { $0.characters.count }
print(stringLenghts)


func checkIfEqual<T: Equatable>(first: T, _ second: T) -> Bool {
    return first == second
}
print(checkIfEqual(first: 1, 1))
print(checkIfEqual(first: "a string", "a string"))
print(checkIfEqual(first: "a string", "a different string"))


func checkIfDescriptionsMatch<T: CustomStringConvertible, U: CustomStringConvertible>(first: T, _ second: U) -> Bool {
    return first.description == second.description
}
print(checkIfDescriptionsMatch(first: Int(1), UInt(2)))
print(checkIfDescriptionsMatch(first: 1, 1.0))
print(checkIfDescriptionsMatch(first: Float(1.0), Double(1.0)))


var myStack = Stack<Int>()
myStack.push(newItem: 10)
myStack.push(newItem: 20)
myStack.push(newItem: 30)

var myStackGenerator = StackGenerator(stack: myStack)
    while let value = myStackGenerator.next() {
        print("Got \(value)")
}
struct Stack<Element>: Sequence { /// ERROR Type Stack<Element> does not conform to protocol 'Sequence' 
   ...
}

Did you make sure that your Stack conforms to the Sequence protocol?

Have a look at the documentation for the Sequence protocol.

I am probably too late here but I struggled with the same issue.

So I opened the documentation manually using Command + Shift + 0 and searched for Sequence. Did a Ctrl - F on ‘iterator’ because ‘GeneratorType’ was changed to ‘IteratorProtocol’.

Found ‘Conforming to the sequence Protocol’. The requirement has changed from generator() to makeIterator().

TLDR -
This:
func makeIterator() -> StackGenerator {
return StackGenerator(stack: self)
}

Not this:
func generate() -> StackGenerator {
return StackGenerator(stack: self)
}