Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

                }
            }