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

Alphonso Sensley II
Alphonso Sensley II
8,514 Points

Need help here!

Hello all, been stuck on this one for a day now. Im pretty sure that the problem lies in the way I'm declaring the computed property self.isEmpty. Specifically the return statement. I just don't know how to fix it. I have looked up some references but can fully find the solution. Any help would be appreciated. Thanks!

generics.swift
struct Queue<Element> {
    var array: [Element]
    var isEmpty: Bool

    init(Element: [Element]) {
        self.array = Element
        self.isEmpty = Bool {
          return .isEmpty }
        }

}

1 Answer

Jeff McDivitt
Jeff McDivitt
23,970 Points

You are on the right track, but you do not need to initialize anything in this challenge. You can replace that with isBool. Then you can check if the array is empty and return true else return false

struct Queue<Element> {

    var array: [Element] = []


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

    }
Alphonso Sensley II
Alphonso Sensley II
8,514 Points

Thanks so much Jeff! I got confused with the language they used in the first task "initialize it to an empty array" Your answer clarified that for me. Thank You!