Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Generics in Swift Generic Types Creating a Generic Type

Kristof Kocsis
Kristof Kocsis
15,455 Points

Code can't compile but no error messages are thrown

I tried to complete this code challenge but I can't pass Task 4. I write my code in xcode and than copy it in the editor. My code works in a playground but the editor doesn't accept it. Am I missing something? Is there something more to the task that I didn't notice?

generics.swift
struct Queue<Element> {

    var array: [Element] = [Element]()

    var isEmpty: Bool {
        if array.count == 0 {
            return true
        } else {
            return false
        }
    }

    var count: Int {
        var counter = 0
        for _ in array {
            counter += 1
        }

        return counter
    }

    mutating func enqueue(element: Element) -> [Element] {
        self.array.append(element)
        return array
    }
}

2 Answers

You are missing a key piece of the requirement omitting the external argument label

mutating func enqueue(_ element: Element) -> [Element] { // Be sure to omit it with _
    self.array.append(element)
    return array
}

struct Queue<Element> { var array = Element var isEmpty: Bool { if array.isEmpty { return true } else { return false } }

var count: Int {
    return array.count
}

mutating func enqueue(_ item: Element) {
    self.array.append(item)
}

mutating func dequeue() -> Element? {
    if self.array.isEmpty == false {
        return self.array.remove(at: 0)
    } else {
        return nil
    }

}

}