mike vorisis mike vorisis - 4 months ago 12
Swift Question

Making a Dictionary in Swift

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

Dictionary
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.includeKey("idEvent")
query.includeKey("eventType")
query.includeKey("idEvent.idMagazi")
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

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
                }
                print(self.events[1]!.nameEvent)
            }

Thank you all for your answers!

Comments