mike vorisis mike vorisis - 1 year ago 70
Swift Question

Making a Dictionary in Swift

I have an object taken from Parse and I want to save its columns into a

or something else (if it's better).

I want to have a Dictionary like this:
["name" : "Mike", "lastname" : "vorisis", "id" : "advsas"]

Below is the code I use to take my results:

func queryEvents() {
let query = PFQuery(className: "eventController")
query.limit = 1000
query.findObjectsInBackgroundWithBlock { (objects, error)-> Void in
if let objects = objects {
for object in objects {
var post = object["idEvent"] as? PFObject
var post2 = post!["idMagazi"]
print("retrieved related post: \(post2["name"]!)")

Answer Source

I finally found it out how can i do it.

I use a struct with what I want like this:

var userDictionary = [Int : Event]()
struct Event {
        var nameEvent: String
        var nameMagazi: String


And then i use this:

  if let objects = objects  {
                for object in objects {

                    let post = object["idEvent"] as? PFObject
                    let post2 = post!["idMagazi"] as? PFObject

                    let nameEvent = post!["name"] as! String
                    let idEvent = post?.objectId
                    let nameMagazi = post2!["name"] as! String

                     self.events[self.i] = Event(nameEvent: nameEvent , nameMagazi: nameMagazi)

                    self.i += 1

Thank you all for your answers!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download