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

Creating a Generic Type: Task 2/5

Hi all,

any help would be appreciated: I'm getting a compiler error, but the preview isn't showing me anything? I've tested it out in a playground and my code functions as intended!

generics.swift
struct Queue<Element>{
    var array: [Element] {
        return []
    }

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

1 Answer

luke jones
luke jones
8,915 Points

You're sort of on track, it's just the way you wrote it. Oddly I don't think the first part of the challenge should of passed. To initialise and empty array you do it like so:

var array = [Element]()

I believe because it returns the same value the challenge passes.

Here's the full answer:

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

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

You can also test your code by doing this:

let emptyArray: [String] = []
let arrayOfInt: [Int] = [1,2,3,4]

Queue.init(array: emptyArray).isEmpty // returns true
Queue.init(array: arrayOfInt).isEmpty // returns false

You wouldn't be able to test it if you didn't initialise it properly

Thanks for the help! flew through the rest of the challenge after that blockage disappeared!