Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Miguel Rivera
Miguel Rivera
21,679 Points

Swift with Parse Relations

Hi!... Im having a problem with register view in my app. In the register View Im displaying a list of program class in a picker view when the user register all the information is saved but I want to save the program class object Id too in another column in User Class. How do i do that? I know is with relation but is not working for me

Thank You!

Miguel Rivera
Miguel Rivera
21,679 Points

This is my code:

override func viewDidLoad() {
    super.viewDidLoad()

    picker.dataSource = self
    picker.delegate = self
    program.inputView = picker

    let query = PFQuery(className:"Program")
    query.whereKey("objectId", equalTo:programId)
    //query.includeKey("objectId")
    query.findObjectsInBackgroundWithBlock
        {
        (programs:[PFObject]?, error:NSError?) -> Void in

        if error == nil
        {
            print("Successfully retrieved \(programs?.count) records.")
            for program in programs! // as [PFObject]

            {
                //let programName = program["program_name"] as PFObject
                if !self.programArray.containsObject(program["program_name"] as! String)

// && !self.array.containsObject(program["objectId"] as! String)) {

                program.objectForKey("program_id")?.objectId
                   // program.objectForKey("major")?.objectId


                    self.programArray.addObject(program["program_name"] as! String)
                   // self.array.addObject(program["objectId"] as! String)

//
//
//
// do{ //
//
// let object = PFObject(className: "_User") // object["program_id"] = PFObject(withoutDataWithClassName: "Program", objectId:"objectId") // object .relationForKey("object_id") .addObject(object) // try object.save() // } catch { // print(error) // }

                }
            }