2

I habe written a function, that updates data in parse

    func updateParse(className: String, whereKey: String, equalTo: String, updateData: Dictionary<String, Any>) {

    let query = PFQuery(className: className)

    query.findObjectsInBackgroundWithBlock {
        (objects, error) -> Void in

        if error == nil {
            if let objects = objects as? [PFObject] {
                for object in objects {
                    query.getObjectInBackgroundWithId(object.objectId!){
                        (prefObj, error) -> Void in
                        if error != nil {
                            print(error)
                        } else if let prefObj = prefObj {

                                for (key, value) in updateData {

                                    prefObj[key] = value // Cannot assign a value of type 'Any' to a value of type 'AnyObject?'

                                }
                            prefObj.saveInBackground()
                        }
                    }
                }
            }
        } else {
            print("Error: \(error!)")
        }

    }

}

i call it with

let imageData = UIImagePNGRepresentation(self.uploadPreviewImage.image!)
let parseImageFile =  PFFile(name: "userProfileImage.png", data: imageData!)

updateParse("ProfileImages", whereKey: "uploader", equalTo: "Phil", updateData: ["imageFile":parseImageFile])

I have commented the error in the corresponding line. It is important to note that the type is not always a picture. Sometimes a string.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
PhilHarmonie
  • 435
  • 1
  • 6
  • 16
  • What part do you not actually understand here? If you don't intend to regular ask questions of mediocre (or worse) quality, it's important to be very clear about what you don't understand about the compiler error or what you've tried to attempt to resolve it. The answer you marked as accepted doesn't explain anything that's going to be much value to anyone later. But if you explain what you don't understand, someone can explain it to you so you don't have to ask in the future. And if you don't have to ask, you're getting more stuff done. – nhgrif Aug 22 '15 at 13:23

2 Answers2

4

If you know that the type is always of type AnyObject you can use a forced cast:

prefObj[key] = value as! AnyObject

Otherwise use an optional cast with as? or change the function signature to:

func updateParse(className: String, whereKey: String, equalTo: String, updateData: Dictionary<String, AnyObject>)
Qbyte
  • 12,753
  • 4
  • 41
  • 57
0

first of all you are overriding the variable 'prefObj ' here

if error != nil {
    print(error)
} else if let prefObj = prefObj { //does not make sence
...
}

try this:

if error != nil {
    print(error)
} else if var prefObj_NotNullUnwrapped = prefObj { // you can use let instead of var
...
}

it's untested but this is the way how to do

var means you can change the reference of the variable stored

let tells the compiler that the variable reference will not change

i am not quite shure if your error really relates on it because you are attempting to change a variable inside a "let" variable

Also your parameter in the message signature does not fit, try changing it to:

func updateParse(className: String, whereKey: String, equalTo: String, updateData: Dictionary<String, AnyObject?>)

See also this stackoverflow question which explains the difference between Any (all types including Int, Double etc.) and AnyObject (only "real" class objects)

The questionmark on AnyObject? tells the compiler that a variable might be without a reference, also known as nil

Community
  • 1
  • 1
Frithjof Schaefer
  • 1,135
  • 11
  • 22
  • With the `if let` he doesn't override the variable. He is shadowing it. That means that `prefObj` is now the unwrapped value instead. The `let` works in this case because `prefObj` is of type `NSDictionary` and therefore a class which properties can be mutated in any case. – Qbyte Aug 22 '15 at 13:50
  • I dont have xcode arround to test and i didnt do alot of swift in the past month. thanks for pointing that out. – Frithjof Schaefer Aug 22 '15 at 13:54
  • You're welcome. Only to clarify: I'm not sure wether your solution would work since the dictionary returns `AnyObject??` instead of `AnyObject?`. – Qbyte Aug 22 '15 at 14:21