Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 661
GuiSoySauce Avatar asked Aug 20 '15 03:08

GuiSoySauce


People also ask

How to convert string to INT in Swift?

A variable can be declared as String type by following the below syntax, Swift provides the function of integer initializers using which we can convert a string into an Int type. To handle non-numeric strings, we can use nil coalescing using which the integer initializer returns an optional integer.

How do I convert a swift set to an array?

Swift Set to Array. An NSSet can be converted to Array using set.allObjects() but there is no such method in the new Set (introduced with Swift 1.2). It can still be done by converting Swift Set to NSSet and use the allObjects() method but that is not optimal.

How do I get a string from a NSObject in Swift?

The toString function accepts any type and will always produce a string. If it’s a Swift type that implements the Printable protocol, or has overridden NSObject ’s description property, you’ll get whatever the .description property returns. In the case of NSNumber, you’ll get a string representation of the number.

What does the toString function do in Swift?

The toString function accepts any type and will always produce a string. If it’s a Swift type that implements the Printable protocol, or has overridden NSObject ’s description property, you’ll get whatever the .description property returns.


1 Answers

When in doubt, try the init!

let selectedItems: Set<String> = ["One", "Two", "Three"]
let arr = [String](selectedItems)
like image 57
Mr Beardsley Avatar answered Oct 26 '22 18:10

Mr Beardsley