Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 474
Mansour Avatar asked Jan 27 '16 20:01

Mansour


1 Answers

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
like image 197
Unis Barakat Avatar answered Nov 16 '22 18:11

Unis Barakat