ios - Swift - Parse.com - 为什么这个闭包是错误的?

标签 ios swift authentication parse-platform closures

试验一个名为 sweeter 的类似 Twitter 应用程序的教程。一切正常,但登录和注册关闭不接受我的论点:

错误信息:

无法使用类型为“(String!, password: String!, (PFUser!, NSError!) -> Void)”的参数列表调用“logInWithUsernameInBackground”

提前致谢

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)


        }
    }

编辑 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
    }

截图 enter image description here

编辑 3.0 错误消息“字符串不可转换为 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
    }

最佳答案

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.
  }
}

在你使用的 block 中 if(PFUser){ } 它应该是 if user {...}

关于ios - Swift - Parse.com - 为什么这个闭包是错误的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31434755/

相关文章:

ios - 如何在 Xcode 中向 OpenGL ES 帧捕获数据添加注释?

django - 如何在用户关闭基于 django2.0 的网站上的选项卡或浏览器时强制注销用户

php - 使用 Instagram API 本地登录

ios - 在没有将 isRemovedOnCompletion 设置为 false 的情况下保留动画的 stokeEnd 更改

ios - 如何在其他类 objective-c 中获取循环值

ios - 向 PHImageManager 请求图像导致 iOS 8.3 中的图像错误

ios - 使用 SwiftUI 以编程方式发送 SMS

ios - persistenceEnabled 在模拟器中工作,但在设备上引发 NSInternalInconsistencyException

json - 对比 Codable 转换前后的 JSON 数据

authentication - ASP.NET 成员资格 : to be or not to be?