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

Johnny Nguyen
Johnny Nguyen
3,875 Points

Challenge Task 2 of Creating a Queue

When adding isEmpty stored property, I'm not sure why I'm getting it wrong

generics.swift
struct Queue<Element> {

   var array: [Element]


  var isEmpty: Bool {
      return array.isEmpty
  }

}

2 Answers

rydavim
rydavim
18,765 Points

I don't know any Swift, so I'm going to have trouble explaining why it works the way it seems to but I'll give it a shot. Assuming you're working on Task 2, it looks like you'll need to manually work out what is returned.

struct Queue<Element> {
  var array = [Element]()
  var isEmpty: Bool {
    // check boolean value using an if statement
    // if true, return true else return false
  }
}

I don't know why the challenge wants you to do it this way. While I can't say for sure as I'm not familiar with the language, it seems to me like your code should work fine assuming isEmpty returns a boolean.

Sometimes challenges want your solution in a specific format and may not pass even if your end result is correct. I would try testing your code in workspaces or your own environment and see if you get the results you expect. If so, they're likely looking for a specific solution. If not, there's something else going on with your isEmpty test.

Sorry I can't be of more help, but hopefully that gives you an idea of how to progress. Good luck, and happy coding!

Johnny Nguyen
Johnny Nguyen
3,875 Points

I put the code in Xcode and it works fine. I have try multiple ways to check if the array is empty but they don't also work. Thanks for your reply anyway.

rydavim
rydavim
18,765 Points

In the interest of getting you through to the next step, the following code seems to work for the purposes of the challenge. (Note that as far as I can tell, the way you're doing it seems not only fine, but probably better.)

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