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

Anthony Lafont
Anthony Lafont
17,075 Points

I can't figure out the answer for this challenge, need help!

I'm stuck in the second task of this challenge. Here is my answer but it doesn't work.

Can someone explain me what to do?

Thanks

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

    var isEmpty: Bool {
        if array[0] == nil {
            return true
        } else {
            return false
        }
    }

}

2 Answers

luke jones
luke jones
8,915 Points

if array[0] == nil

Checks whether the first value of the array is nil, so the second value, third value and so on can still be a value rather than nil.

What you need is

if array.count == 0

That checks the count of the array. if the count is 0, then the array is therefore empty and true can be returned

Anthony Lafont
Anthony Lafont
17,075 Points

Thank you very much, it helps a lot!