I have a Swift app with a view that has a text field and a button. When the button is clicked, it invokes the method below.
@IBAction func AddClick(sender: AnyObject) {
employeeManager.AddFirstName(firstName.text)
}
I'm not sure why I keep getting the following error:
Cannot invoke 'AddFirstName' with an argument list of type '(String!)'
Is it because I cannot implicitly convert type String! to String?
AddClick definition:
var employeeManager: EmployeeManager = EmployeeManager()
class EmployeeManager {
func AddFirstName(firstName: String){}
Even if I change the code to
func AddFirstName(firstName: String!) {}
it still doesn't work.
What would be the best way to do this?
-- EDIT --
I see the problem. It is something to do with the following line in the EmployeeManager class:
var employeeManager: EmployeeManager = EmployeeManager()
If I do this in the AddClick, it works:
@IBAction func AddClick(sender: AnyObject) {
var e = EmployeeManager()
e.AddFirstName(firstName.text)
}
This line is meant to act like a singleton and provide app wide access to the EmployeeManager class. Did I do this wrong?
var employeeManager: EmployeeManager = EmployeeManager()
The issue is in my use of
var employeeManager: EmployeeManager = EmployeeManager()
Going with a Singleton pattern, which the above is really supposed to behave like, resolves this issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With