0
votes

Experimentig a tutorial of a Twitter-like app called sweeter. Everything is ok, but the login and sign up closures does not accept my arguments:

error message:

Cannot invoke 'logInWithUsernameInBackground' with an argument list of type '(String!, password: String!, (PFUser!, NSError!) -> Void)'

thanks in advance

import UIKit
import Parse

class TimelineTableViewController: UITableViewController {

//    MARK: Parse
    override func viewDidAppear(animated: Bool) {

        if PFUser.currentUser() == nil {
        var loginAlertController = UIAlertController(title: "Sign up / login", message: "please sign up or login", preferredStyle: UIAlertControllerStyle.Alert)

            loginAlertController.addTextFieldWithConfigurationHandler({
                textfField in
                textfField.placeholder = "Your username"
            })

            loginAlertController.addTextFieldWithConfigurationHandler({
                textfField in
                textfField.placeholder = "Your password"
                textfField.secureTextEntry = true
            })

//            MARK: login action in the array
            loginAlertController.addAction(UIAlertAction(title: "Login Action", style: UIAlertActionStyle.Default, handler: {
                alertAction in
                let textFields : NSArray = loginAlertController.textFields!
                let usernameTextField : UITextField = textFields[0] as! UITextField
                let passwordTextField : UITextField = textFields[1] as! UITextField

                //MARK: Parse login problem - 15:39
                PFUser.logInWithUsernameInBackground(usernameTextField.text, password: passwordTextField.text){
                    (user: PFUser?, error: NSError?) -> Void in

                    if (PFUser) {
                        println("login success!")
                    } else {
                        println()("login failed!")
                    }
                }
            }))

//            MARK: sign up action in the array
            loginAlertController.addAction(UIAlertAction(title: "Sign up", style: UIAlertActionStyle.Default, handler: {
                alertAction in
                let textFields : NSArray = loginAlertController.textFields!
                let usernameTextField : UITextField = textFields[0] as! UITextField
                let passwordTextField : UITextField = textFields[1] as! UITextField

                var sweeter = PFUser() //16:42
                sweeter.username = usernameTextField.text
                sweeter.password = passwordTextField.text

                sweeter.signUpInBackgroundWithBlock({
                    (success: Bool, error: NSError?) -> Void in
                    if error == nil {
                        println("sign up successful")
                    } else {
                        let errorString = error!.userInfo["error"] as! String
                        println(errorString)
                    }
                })

            }))


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


        }
    }

EDIT 2.0:

import UIKit
import Parse

class TimelineTableViewController: UITableViewController {


    var timeLineData : [String] = []

    func loadData() {
        timeLineData.removeAll(keepCapacity: true)

        var findTimelineData = PFQuery(className: "Sweeters")

        findTimelineData.findObjectsInBackgroundWithBlock({
            (objects : [AnyObject]?, error : NSError?) -> Void in

            if error == nil {
                for object in objects! {
                    self.timeLineData.append(object as! String)
                }
                let array : Array = self.timeLineData.reverse()
                self.timeLineData = array as Array

                self.tableView.reloadData()
            }
        })
    }





    //    MARK: Parse
    override func viewDidAppear(animated: Bool) {

        if PFUser.currentUser() == nil {
            var loginAlertController = UIAlertController(title: "Sign up / login", message: "please sign up or login", preferredStyle: UIAlertControllerStyle.Alert)

            loginAlertController.addTextFieldWithConfigurationHandler({
                textfField in
                textfField.placeholder = "Your username"
            })

            loginAlertController.addTextFieldWithConfigurationHandler({
                textfField in
                textfField.placeholder = "Your password"
                textfField.secureTextEntry = true
            })

            //            MARK: login action in the array
            loginAlertController.addAction(UIAlertAction(title: "Login Action", style: UIAlertActionStyle.Default, handler: {
                alertAction in
                let textFields : NSArray = loginAlertController.textFields!
                let usernameTextField : UITextField = textFields[0] as! UITextField
                let passwordTextField : UITextField = textFields[1] as! UITextField

                //MARK: Parse login problem - 15:39
                PFUser.logInWithUsernameInBackground(usernameTextField.text, password: passwordTextField.text){
                    (user: PFUser?, error: NSError?) -> Void in

                    if user != nil {
                        println("login success!")
                    } else {
                        println("login failed!")
                    }
                }
            }))

            //            MARK: sign up action in the array
            loginAlertController.addAction(UIAlertAction(title: "Sign up", style: UIAlertActionStyle.Default, handler: {
                alertAction in
                let textFields : NSArray = loginAlertController.textFields!
                let usernameTextField : UITextField = textFields[0] as! UITextField
                let passwordTextField : UITextField = textFields[1] as! UITextField

                var sweeter = PFUser() //16:42
                sweeter.username = usernameTextField.text
                sweeter.password = passwordTextField.text

                sweeter.signUpInBackgroundWithBlock({
                    (success: Bool, error: NSError?) -> Void in
                    if error == nil {
                        println("sign up successful")
                    } else {
                        //                        let errorString = error!.userInfo["error"] as! String
                        let errorString = error!.localizedDescription
                        println(errorString)
                    }
                })

            }))


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


        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return timeLineData.count
    }


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

        // Configure the cell...

        return cell
    }

screenshot enter image description here

EDIT 3.0 error message "string is not convertible to PFObject"

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("cellReuseID", forIndexPath: indexPath) as! SweetTableViewCell

        // Configure the cell...

        let sweet : PFObject = self.timeLineData[indexPath.row] as PFObject

        cell.sweetTextView.text = sweet.objectForKey("content") as! String

        return cell
    }
3
try replacing PFUser! and NSError! with PFUser? and NSError? โ€“ s1ddok

3 Answers

0
votes

I guess you should replace PFUser! and NSError! with PFUser? and NSError?

And also, have a closer look at this closure :

sweeter.signUpInBackgroundWithBlock({
                    (success: Bool(), error: NSError()) -> Void in
                    if error == nil {
                        println("sign up successful")
                    } else {
                        let errorString = error.userInfo["error"] as String
                        println(errorString)
                    }
                })

I guess closure parameters should be (success: Bool, error: NSError?) -> Void

Edit: first error is because you cant do like that in Swift

 if (PFUser) {

Should be user != nil

This line has extra () pair println()("login failed!")

Second error is fixed by replacing

let errorString = error.userInfo["error"] as String

With let errorString = error!.localizedDescription

1
votes
PFUser.logInWithUsernameInBackground("myname", password:"mypass") {
  (user: PFUser?, error: NSError?) -> Void in
  if user != nil {
    // Do stuff after successful login.
  } else {
    // The login failed. Check error to see why.
  }
}

in the block you have used if(PFUser){ } it is supposed to be if user {...}

1
votes
 var timeLineData : [String] = []

    func loadData() {
        timeLineData.removeAll(keepCapacity: false)

        var findTimelineData = PFQuery(className: "Sweeters")

        findTimelineData.findObjectsInBackgroundWithBlock({
            (objects : [AnyObject]?, error : NSError?) -> Void in
            if error == nil {
                  // The find succeeded.
               println("Successfully retrieved \(objects!.count) objects.")
              // Do something with the found objects
             if let objects = objects as? [PFObject] {
                   for object in objects {
                     self.timeLineData.append(object as! String)
                  }
                }
               } else {
                  // Log details of the failure
                   println("Error: \(error!) \(error!.userInfo!)")
                  }
             })

      self.tableview.reloadData()
       }