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

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,814 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,814 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
    }
}