0

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(){ 
9
  • 1
    If postStruct is actually a Struct like its name implies, you should capitalize it as PostStruct; that's a Swift convention, so following it makes things clearer for everyone. Does PostStruct have an init that takes both a title and a message? Commented Aug 20, 2017 at 20:40
  • Post struct is just the name of the struct & no it doesn't have an init. @Connor Commented Aug 20, 2017 at 20:46
  • Use autocompletion feature to insert the postStruct into the array. also if it doesn't work, try postStruct.init and that should autocomplete the arguments and satisfy the compiler and yourself. Commented Aug 20, 2017 at 20:48
  • there's no autocompletion suggestions also, I tried to input 'postStruct.init' and I got the error "Can not call value of non-function type 'postStruct' @DominikBucher Commented Aug 20, 2017 at 21:01
  • @LaniMJ then take the object initialization out of the insert function and try again. Commented Aug 20, 2017 at 21:04

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I added the replacement "self.posts.insert(PostStruct(title: title ?? "", message: message ?? ""), at: 0)" but now I'm getting the error "Argument passed to call that takes no arguments"
Have you replaced the definition of PostStruct as shown?
I really do appreciate the help, I'm new at Xcode and I know I probably sound like a babbling idiot! @OOPer
and yes! @OOPer
I cannot reproduce the error Argument passed to call that takes no arguments in my Xcode. If Clean Build does not work, please add your latest code into your question (this time you should not remove the old code, as it is explaining the problem shown in the title.)
|
0

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 } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.