jeudi 25 juin 2015

Error: 'NSInvalidArgumentException' ... unrecognized selector sent to instance

Here is my problem, I'm trying to make a clone of Snapchat, a very simple one, using Parse.com with the default project given in the page, everything was going ok, until I have to take pictures and save them to send them to an user. With this code:

import UIKit
import Parse

class UsersTableViewController: UITableViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {


    var usersArray: [String] = []

    var activeRecipient: Int = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        var query = PFUser.query()
        query?.whereKey("username", notEqualTo: PFUser.currentUser()!.username!)

        var users = query?.findObjects()

        if let user = users {
            for username in user {
                println(username.username!!)

                usersArray.append(username.username!!)


            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return usersArray.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("snapCell", forIndexPath: indexPath) as! UITableViewCell

        cell.textLabel?.text = usersArray[indexPath.row]

        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        activeRecipient = indexPath.row

        pickImage()


    }

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

        //Upload image to parse //error come somewhere from here I think


        println("Image Selected")
        self.dismissViewControllerAnimated(true, completion: nil)


        var imageToSend = PFObject(className:"Image")
        imageToSend["image"] = UIImageJPEGRepresentation(image, 0.5)
        imageToSend["senderUsername"] = PFUser.currentUser()!.username
        imageToSend["recipientUsername"] = usersArray[activeRecipient]

        imageToSend.save()

    }

    func pickImage() {

        var image = UIImagePickerController()
        image.delegate = self
        image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        image.allowsEditing = false

        self.presentViewController(image, animated: true, completion: nil)

    }


}

The error:

2015-06-24 15:03:13.414 SnapClomSwift[2043:124661] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableData PF_base64EncodedString]: unrecognized selector sent to instance 0x7a648960'

Is not very helpful to debug, any help??

EDIT1: I would think the parse function is the last calling and breaking everything but I'm not sure.

screenshot of the exception

EDIT2: I have I guess fixed but I'm not sure what was in the first place, still. The new code is this:

var imageToSend = PFObject(className:"Image")
        //var imageData = UIImagePNGRepresentation(image)
        imageToSend["senderUsername"] = PFUser.currentUser()!.username!
        imageToSend["recipientUsername"] = usersArray[activeUser]
        imageToSend.saveInBackgroundWithBlock({
            (success: Bool, error: NSError?) -> Void in
            if (success == false) {
                // Error.
                println("Error horray! \(error?.description)")
            } else {
                // There was a problem, check error.description

                let imageData = UIImagePNGRepresentation(image)

                let imageFile = PFFile(name: "image-png", data: imageData)

                imageToSend["image"] = imageFile

                imageToSend.saveInBackgroundWithBlock({
                    (success: Bool, error: NSError?) -> Void in

                    if success == false {

                        println("something is fucked up \(error?.description)")
                    } else {

                        println("Cool")
                    }
                })

                println("Done")
            }
        })

I guess this is not a solution but a workaround so I'm going to accept Zaph's answer.

See more ..

Aucun commentaire:

Enregistrer un commentaire