Search code examples
stringswiftsetstring

Convert populated Set<String> to [String] in swift


I am kind stuck with the following.

I've got the following array

var selectedItems = Set<String>()

That has some items from parse inserted to it

self.selectedItems.insert(objectToAppend)

Then I've created the following variable where I intend to convert the Set into something I could use on my parse query with the following line. But almost nothing converts. [AnyObject, [String], [self.selectedHobbies], ["\(self.selectedHobbies)"] ... none works.

let itemsArray = self.selectedHobbies as [AnyObject]

And if I do not convert it I cannot use in the query bellow.

query.whereKey("itemTag", containedIn: itemsArray as [AnyObject])

If I could manage to convert it to a [String] it would solve my problem. Not sure how.


Solution

  • When in doubt, try the init!

    let selectedItems: Set<String> = ["One", "Two", "Three"]
    let arr = [String](selectedItems)