简体   繁体   中英

Saving Arrays to and retrieving from Parse in swift

My app contains WordLists with associated Words in CoreData. I can upload them to Parse to share them, but i am struggling to get them out from Parse again and back into CoreData.

To save all the words associated to a wordlist to parse I have:

parseWordList.setObject("\(wordList?.words?.valueForKey("wordName"))", forKey: "Words") 

which saves as a string, but looks like a set in Parse which has confused me?

在此处输入图片说明

Then when I query the list again I am struggling to covert to an array so I can iterate through it and save it back into CoreData.

How do I convert the Parse Words string/set? back to an array, and how do I get rid of the 'Optional' prefix when saving to Parse?

Thanks in advance to anyone who can help me...

let query = PFQuery(className: "WordList")
    query.findObjectsInBackgroundWithBlock { (objects:[PFObject]?, error:NSError?) -> Void in

        if error == nil && objects != nil {
                for object in objects! {

                    let joinedWords = object.objectForKey("Words")
                    let joinedWordsArray = Array(arrayLiteral: joinedWords)

                    let wordEntity = NSEntityDescription.entityForName("Word", inManagedObjectContext: self.coreDataStack.managedObjectContext)

                    for joinedWord in joinedWordsArray {
                        let word = Word(entity: wordEntity!, insertIntoManagedObjectContext: self.coreDataStack.managedObjectContext)
                        word.wordName = joinedWord as? String
                        word.wordList = joinedWordList
    }
                }
        } else {
            print("Error")
        }
    }

在此处输入图片说明

instead of:
let joinedWordsArray = Array(arrayLiteral: joinedWords)

try let joinedWordsArray = joinedWords as! Array

hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM