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

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!