Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
    }

}

}