0

I'm trying to create a simple ios app and I keep getting an error as "Extra argument in call" when I'm passing the exact same values.

TaskManager.swift

struct task { var name = "Name" var desc = "Description" } var tasks = [task]() class TaskManager: NSObject { func addTask(name: String, desc: String) { tasks.append(task(name: name, desc: desc)) } } 

The calling function is in another swift file,

class SecondViewController: UIViewController, UITextFieldDelegate { @IBOutlet weak var txtTask: UITextField! @IBOutlet weak var txtDesc: UITextField! @IBAction func btnAddTask(sender: UIButton) { if (txtTask.text != "") { TaskManager.addTask(txtTask.text, txtDesc.text) txtTask.text = nil txtDesc.text = nil } } } 

I'm sending the exact same arguments. What am I doing wrong?

1
  • structs should be named starting with an uppercase letter Commented Feb 11, 2016 at 14:34

2 Answers 2

2

Right now it's expecting 1 argument, and that's an initialised Task Manager. You aren't initialising the Task Manager. If you want to do it like you are (not initialising it), make your method static:

static func addTask(etc.. 

Then you can call it like so:

TaskManager.addTask(2 args... 

Or, keep it how it is and just initialise TaskManager:

let manager = TaskManager() manager.addTask(etc... 
Sign up to request clarification or add additional context in comments.

Comments

0
struct Task { var name: String var desc: String } var tasks: [Task] = [] class TaskManager: NSObject { static func addTask(name: String, desc: String) { tasks += [task(name: name, desc: desc)] } } TaskManager.addTask("someName", desc: "shouldn't this be a bool?") print(tasks) // [task(name: "someName", desc: "shouldn\'t this be a bool?")] 

Add static in front of addTask(_:) and it will work.

4 Comments

Haha, we were about the same I think. The only reason I posted this as an answer is his code was too messy and was itching me :p
structs should be named starting with an uppercase letter
@LeoDabus: Thanks, I missed that one :)
@Eendje Thanks for the answer! I'll try to make my code look neat in the future :p

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.