LaniMJ
LaniMJ

Reputation: 3

Extra Argument In Call?

I've been trying to resolve this "EXTRA ARGUMENT 'title' IN CALL" ALL week. Below is the code with the error I'm having trouble with. I've added the new code that I'm currently working with in Xcode. The error I'm getting with this code is:

"ARGUMENT PASSED TO CALL THAT TAKES NO ARGUMENTS"

NEW CODE

import UIKit
import Firebase
import FirebaseDatabase

struct PostStruct {
    struct PostStruct {
        let title: String
        let message : String
    }
}

class DatabaseViewController: UITableViewController {
    var posts: [PostStruct] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        //
        let databaseRef = Database.database().reference()

        databaseRef.child("Posts").queryOrderedByKey().observe(.childAdded, with: {
            snapshot in

            let snapshotValue = snapshot.value as? NSDictionary
            let title = snapshotValue?["title"] as? String

            let message = snapshotValue?["message"] as? String

            self.posts.insert(PostStruct(title: title ?? "", message: message ?? ""), at: 0) **// <-- ARGUMENT PASSED TO CALL THAT TAKES NO ARGUMENTS**
            self.tableView.reloadData()
        })
        post()
    }

    func post(){
        let title = "Title"
        let message = "Message"

        let post : [String : AnyObject] = ["title" : title as AnyObject, "message" : message as AnyObject]

         let databaseRef  = Database.database().reference()

        databaseRef.child("Posts").childByAutoId().setValue(post)
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")

        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
            cell?.textLabel?.text = "New value"
            cell?.detailTextLabel?.text = "New value"
            return cell!
        } else {
            cell?.textLabel?.text = "" //reset value
            cell?.detailTextLabel?.text = "" // resetValue
            cell?.textLabel?.text = "New value"
            cell?.detailTextLabel?.text = "New value"
            return cell!
        }
    }
}

OLD CODE

import UIKit
import Firebase
import FirebaseDatabase

struct PostStruct {
    let title = String!.self
    let message : String!
}

class DatabaseViewController: UITableViewController {
    var posts = [postStruct]()

    override func viewDidLoad() {
        super.viewDidLoad()
        //
        let databaseRef = Database.database().reference()

        databaseRef.child("Posts").queryOrderedByKey().observe(.childAdded, with: {
            snapshot in

            let snapshotValue = snapshot.value as? NSDictionary
            let title = snapshotValue?["title"] as? String

            let message = snapshotValue?["message"] as? String

            self.posts.insert(PostStruct(title: title ,message: message), at: 0) // **<--  EXTRA ARGUMENT 'title' IN CALL**
            self.tableView.reloadData()
        })
        post()
    }

    func post(){

Upvotes: 0

Views: 3339

Answers (2)

OOPer
OOPer

Reputation: 47876

Your code has two critical flaws:

The definition of PostStruct should be something like this:

struct PostStruct {
    let title: String
    let message : String
}

The line let title = String!.self declares your title as having a class object, not String.

And another is the line DatabaseViewController:

var posts = [post]

In your code, post is a method, I believe you do not want an Array of methods.

Which should be something like this:

var posts: [PostStruct] = []

And to make two fixes above to work, you need a little more:

        self.posts.insert(PostStruct(title: title ?? "", message: message ?? ""), at: 0)

You may have some other faults in your code, but at least you need the fixes above. Try them.

Upvotes: 2

Charles Srstka
Charles Srstka

Reputation: 17040

Your PostStruct struct has a default value for the title property. The automatically-generated initializer will only contain parameters for values that don't already have a default, which is why the automatic initializer has message as a parameter and not title. Also, your default value for title is the String type itself, rather than an instance of a string, which I doubt is what you want. I also doubt that the properties of PostStruct really need to be implicitly-unwrapped optionals.

Declare the struct like this, and your initializer should work:

struct PostStruct {
    let title: String
    let message: String
}

Upvotes: 0

Related Questions