将数组保存到swift中并从Parse中检索

时间:2016-01-05 10:57:09

标签: arrays swift core-data parse-platform

我的应用包含在CoreData中包含关联词的WordLists。我可以将它们上传到Parse以共享它们,但我正在努力将它们从Parse中再次发送回CoreData。

要保存与wordlist相关联的所有单词以进行解析,我有:

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

以字符串形式保存,但在Parse中看起来像是一个让我感到困惑的集合?

enter image description here

然后当我再次查询列表时,我正在努力转换为数组,因此我可以迭代它并将其保存回CoreData。

如何转换Parse Words字符串/ set?回到数组,如何在保存到Parse时删除'Optional'前缀?

提前感谢任何可以帮助我的人......

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")
        }
    }

enter image description here

1 个答案:

答案 0 :(得分:0)

而不是:
    让joinWordsArray = Array(arrayLiteral:joinedWords)

试     让joinWordsArray = joinWords为!数组

希望这会有所帮助