NSDictionary - cannot call "init"

This is the code

let dictionary = NSDictionary(objects: [user.username, image], forKeys: ["username", "image"])


I am getting the error

Can't call "init" with argument list type (objects: $ T3, forKeys: $ T7)

Additional code to show how username and images are generated

let users = objects

                        for userId in self.arrayUserIds
                            let user = self.getUserFromUserId(userId as String, arrayUsers: users)


                            var query = PFQuery(className: "UserPhoto")
                            query.whereKey("user", equalTo: user)

                                (NSArray objects, NSError error) -> Void in

                                if(objects.count != 0)
                                    let object = objects[objects.count - 1] as PFObject
                                    let theImage = object["imageData"] as PFFile

                                    let imageData:NSData    = theImage.getData()
                                    let image               = UIImage(data: imageData)

                                    let dictionary = NSDictionary(objects: [user.username, image], forKeys: ["username", "image"])




source to share

1 answer

The UIImage initializer you call returns an optional parameter

init?(data data: NSData)


From the docs: Return value An initialized UIImage object, or nil if the method was unable to initialize an image from the specified data.

You need to force expand the optional option (check the ! After the image):

let dictionary = NSDictionary(objects: [user.username, image!], forKeys: ["username", "image"])




All Articles