PhilHarmonie
PhilHarmonie

Reputation: 435

Swift: Cannot assign a value of type 'Any' to a value of type 'AnyObject?'

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.

Upvotes: 2

Views: 3717

Answers (2)

Frithjof Schaefer
Frithjof Schaefer

Reputation: 1205

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

Upvotes: 0

Qbyte
Qbyte

Reputation: 13283

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

Upvotes: 4

Related Questions