1

I am trying to create an array of words from a string object retrieved from Parse. The object retrieved looks like this:

img1

Then this line of code gives this.

let joinedWords = object["Words"] as! String

img2

How do I convert joinedWords to an Array?

2 Answers 2

3

If you don't care about the order, you can use flatMap on the set:

var mySet = Set<String>()

for index in 1...5 {
    mySet.insert("testwords\(index)")
}

let myArray = mySet.flatMap { $0 }

print(myArray) // "["testwords5", "testwords3", "testwords4", "testwords2", "testwords1"]"

If you want the list sorted alphabetically, you can make your array a var and use sortInPlace()

var myArray = mySet.flatMap { $0 }

myArray.sortInPlace()

print(myArray) // "["testwords1", "testwords2", "testwords3", "testwords4", "testwords5"]"

If object["Words"] is AnyObject, you will have to unwrap it.

if let joinedWordsSet = object["Words"] as? Set<String> {

    var joinedWordsArray = joinedWordsSet.flatMap { $0 }

    myArray.sortInPlace()

    print(myArray)

}

Swift 3 note: sortInPlace() has been renamed sort().

Sign up to request clarification or add additional context in comments.

7 Comments

ah, no it hasn't. I need to get the words that are in joinedWords string, not create a new set. How would I do that?
@richc It's not clear what you're asking. From your screenshot it looks like Words is a set of Strings ("testwords1", "testwords2", ...). Are you trying to create a string with all of these words?
joinedWords is a set of strings in the form { (testword1, testword2, testword3, testword4, testword5)}. I need to convert that to an array in the form ["testword1", "testword2", "testword3", "testword4", "testword5"]. Order is not important for now. There can be any number of words.
@richc Right, so that's what my answer does. First declare let joinedWords = object["Words"] as! Set<String>, then call flatMap on joinedWords like this: joinedWords.flatMap { $0 } to flatten the set into an array. If object["Words"] is not a Set, tell me what type it is by printing object["Words"].dynamicType.
print(object["Words"].dynamicType) gives ImplicitlyUnwrappedOptional<AnyObject> in console. Declaring joinedWords as a Set<String> crashes with Could not cast value of type '__NSCFString' (0x1e68da4) to 'NSSet' (0x1e697e0).
|
1

Many thanks to @JAL for so much time on chat to solve this one. This is what we came up with. Its a bodge and no doubt there is a better way!

When uploading to Parse save the set as an array.

let wordsSet = (wordList?.words?.valueForKey("wordName"))! as! NSSet
                let wordsArray = Array(wordsSet)

Then it saves to Parse - looking like a set, not an array or a dictionary.

let parseWordList = PFObject(className: "WordList")
parseWordList.setObject("\(wordsArray)", forKey: "Words")
parseWordList.saveInBackgroundWithBlock { (succeeded, error) -> Void in
                    if succeeded {
                       // Do something
                    } else {
                        print("Error: \(error) \(error?.userInfo)")
                    }
                }

Then you can drop the [ ] off the string when its downloaded from Parse, and remove the , and add some "" and voila, there is an array that can be used e.g. to add to CoreData.

var joinedWords = object["Words"] as! String
joinedWords = String(joinedWords.characters.dropFirst())
                        joinedWords = String(joinedWords.characters.dropLast())
let joinedWordsArray = joinedWords.characters.split() {$0 == ","}.map{ String($0) } // Thanks @JAL!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.