Search code examples
arraysswiftparse-platformparse-server

Change the value of an element in a PFUser Object of type Array, in Swift


I'm new to Parse server. First, I create a multidimensional array as an object on Parse server

let day1 = ["one", "two", "three", "four"]
let day2 = ["five", "six", "seven", "eight"]

let jan = [day1, day2]
let feb = [day1, day2]

let calendar = [jan, feb]

let user = PFUser()
user.username = userNameTextField.text
user.password = passwordTextField.text
user["Calendar"] = calendar

enter image description here

What I want is to be able to do a PFUser query and change the value of calendar [ 1 ] [ 0 ] [ 1 ] from "two" to "zero". I've gone over Parse's documentation, but I'm just confused on how I could accomplish this. Thank you for your help!


Solution

  • Here's the code that accomplishes what I was trying to do:

    @IBOutlet weak var bedTimeTextField: UITextField!
    
        var userCalendar = PFUser.current()?.object(forKey: "Calendar") as! Array<Array<Array<Any>>>
    
        func setBedTime(int: Int) {
    
            userCalendar[1][1][int] = 0
    
            PFUser.current()?.setValue(userCalendar, forKey: "Calendar")
    
            PFUser.current()?.saveInBackground()
    
        }
    
        @IBAction func cmonButton(_ sender: Any) {
    
            if bedTimeTextField.text != "" {
    
                let textFieldInt: Int? = Int(bedTimeTextField.text!)
    
                setBedTime(int: textFieldInt!)
    
                let userCalendar = PFUser.current()?.object(forKey: "Calendar") as! Array<Array<Array<Any>>>
    
                print(userCalendar)
    
            }