Mansour
Mansour

Reputation: 535

Contextual type 'AnyObject' cannot be used with array literal

I'm trying to upgrade to upgrade my project to Swift 2 but I stuck on the following error:

Contextual type 'AnyObject' cannot be used with array literal

Here's my code:

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {

    let data = UIImageJPEGRepresentation(image, 0.08)
    let file = PFFile(data: data!)

    PFUser.currentUser()!["Picture"] = [file]
    try! PFUser.currentUser()!.save()}

And here’s the line where the problem occur

        PFUser.currentUser()!["Picture"] = [file]

Thanks a lot for your help !! (I'm beginner,...)

Upvotes: 11

Views: 10794

Answers (1)

Unis Barakat
Unis Barakat

Reputation: 886

substitute this line:

PFUser.currentUser()!["Picture"] = [file]

with:

PFUser.currentUser()!["Picture"] = file

Edit: As noted, it is better practice to not force unwrap a conditional and do something as follows:

guard let user = PFUser.currentUser() else {
    return
}
user["Picture"] = file

Upvotes: 5

Related Questions