简体   繁体   中英

Retrieving Arrays from Parse and save them in a Dictionary with a corresponding value. [String(): [String]()]

I am a beginner here and I'm getting problem in retrieving arrays from parse.. My problem is as follows..

I've made a class called "TypeOfNumbers"

var typeOfNumbers:PFObject = PFObject(className: "TypeOfNumbers")
    typeOfNumbers["type"] = "Prime"
    typeOfNumbers["numbers"] = ["2", "3", "5", "7", "11", "13"]
    typeOfNumbers.saveInBackground()

now let say we have another row in "TypeOfNumbers" class as follows:

var typeOfNumbers:PFObject = PFObject(className: "TypeOfNumbers")
    typeOfNumbers["type"] = "Squares
    typeOfNumbers["numbers"] = ["1", "4", "9", "16", "25"]
    typeOfNumbers.saveInBackground()

now i want to retrieve these two arrays with their corresponding type also..

let say i want to retrieve these arrays and want to save in a dictionary like:

[Prime: ["2", "3", "5", "7", "11", "13"], Squares: ["2", "3", "5", "7", "11", "13"]]

You could try to write this code:

var dictionary = [String:String]
var number = 0
let typeOfNumbersAsArrayOfStrings = typeOfNumbers["numbers"] as! [String]
let typeOfNumbersAsArrayOfStrings2 = typeOfNumbers["numbers"] as! [String]
for object in typeOfNumbersAsArrayOfStrings {
     dictionary["\(object)"] = typeOfNumbersAsArrayOfStrings2[number]
     ++number
}

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