LaniMJ LaniMJ - 1 month ago 19
Swift Question

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(){

Answer Source

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.