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

Creating a Generic Type: Task 2/5

Hi all,

any help would be appreciated: I'm getting a compiler error, but the preview isn't showing me anything? I've tested it out in a playground and my code functions as intended!

generics.swift
struct Queue<Element>{
    var array: [Element] {
        return []
    }

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

1 Answer

luke jones
luke jones
8,915 Points

You're sort of on track, it's just the way you wrote it. Oddly I don't think the first part of the challenge should of passed. To initialise and empty array you do it like so:

var array = [Element]()

I believe because it returns the same value the challenge passes.

Here's the full answer:

struct Queue<Element>{
var array = [Element]()

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

You can also test your code by doing this:

let emptyArray: [String] = []
let arrayOfInt: [Int] = [1,2,3,4]

Queue.init(array: emptyArray).isEmpty // returns true
Queue.init(array: arrayOfInt).isEmpty // returns false

You wouldn't be able to test it if you didn't initialise it properly

Thanks for the help! flew through the rest of the challenge after that blockage disappeared!