无法使用类型'(([AnyObject]!,NSError!) - > Void)'的参数列表调用'findObjectsInBackgroundWithBlock'

时间:2015-04-15 11:21:18

标签: ios swift syntax-error

我正在关注Youtube上名为How to Build a Simple iOS Chat App的教程,该教程由名为Code With Chris的频道制作。我跟着他的所有类型,我在Xcode中得到以下错误:

/Users/David/Documents/360Drive/Xcode/Try/Learn With Chris/ChatApp/ChatApp/ChatApp/ViewController.swift:53:15: Cannot invoke 'findObjectsInBackgroundWithBlock' with an argument list of type '(([AnyObject]!, NSError!) -> Void)'

这是我的代码:

//
//  ViewController.swift
//  ChatApp
//
//  Created by David Chen on 15/4/12.
//  Copyright (c) 2015 cwsoft. All rights reserved.
//

import UIKit
import Parse

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {

    var messagesArray:[String] = [String]()

    @IBOutlet weak var MessageTableView: UITableView!
    @IBOutlet weak var ButtonSend: UIButton!
    @IBOutlet weak var DockViewHeightConstraint: NSLayoutConstraint!
    @IBOutlet weak var MessageTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        //Set delegate
        self.MessageTableView.delegate = self
        self.MessageTableView.dataSource = self
        self.MessageTextField.delegate = self

        let tapGesture:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tableViewTapped")
        self.MessageTableView.addGestureRecognizer(tapGesture)

        self.messagesArray.append("Test 1")
        self.messagesArray.append("Test 2")
        self.messagesArray.append("Test 3")
    }

    @IBAction func ButtonSendPressed(sender: UIButton) {
        self.MessageTextField.endEditing(true)

        var newMessageObject:PFObject = PFObject(className: "Message")
        newMessageObject["Text"] = self.MessageTextField.text
        newMessageObject.saveInBackgroundWithBlock {
            (success: Bool, error: NSError?) -> Void in
            if (success == true) {
                NSLog("Success")
            } else {
                NSLog("Error")
            }
        }
    }

    func retrieveMessages() {
        var query:PFQuery = PFQuery(className: "Messages")
        query.findObjectsInBackgroundWithBlock {
            (object: [AnyObject]!, error: NSError!) -> Void in
            self.messagesArray = [String]()
            for messageObject in objects {
                let messageText:String? = (messageObject as PFObject)["Text"] as? String
                if messagetext != nil {
                    self.messagesArray.append(messageText!)
                }
            }
        }
        self.MessageTableView.reloadData()
    }

    func tableViewTapped() {
        self.MessageTextField.endEditing(true)
    }

    //MARK : TextField Delegage Methods

    func textFieldDidBeginEditing(textField: UITextField) {
        self.view.layoutIfNeeded()
        UIView.animateWithDuration(0.5, animations: {
            self.DockViewHeightConstraint.constant = 320
            self.view.layoutIfNeeded()
            }, completion: nil)
    }

    func textFieldDidEndEditing(textField: UITextField) {
        self.view.layoutIfNeeded()
        UIView.animateWithDuration(0.5, animations: {
            self.DockViewHeightConstraint.constant = 60
            self.view.layoutIfNeeded()
            }, completion: nil)
    }

    //MARK : Table View Delegate Methods

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.MessageTableView.dequeueReusableCellWithIdentifier("MessageCell") as! UITableViewCell
        cell.textLabel?.text = self.messagesArray[indexPath.row]
        return cell
    }

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

这是教程的链接 link

感谢任何有用的建议:)

1 个答案:

答案 0 :(得分:-1)

最后一次xCode更新发生了很多变化。我无法解释为什么需要做一些改变。你最好阅读changeLogs!我稍后会:) 在此期间尝试这个:

func retrieveMessages() {
    var query:PFQuery = PFQuery(className: "Messages")
    query.findObjectsInBackgroundWithBlock {
        (object, error) -> Void in
        self.messagesArray = [String]()
        for messageObject in object! {
            let messageText:String? = (messageObject as! PFObject)["Text"] as? String
            if messageText != nil {
                self.messagesArray.append(messageText!)
            }
        }
    }
    self.MessageTableView.reloadData()
}

在某些情况下,您似乎不再需要指定类型。在其他情况下,你需要。在这种情况下,没有必要,但您需要打开对象数组。