Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting String! to String

Tags:

swift

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()
like image 860
4thSpace Avatar asked Feb 19 '26 20:02

4thSpace


1 Answers

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.

like image 189
4thSpace Avatar answered Feb 21 '26 09:02

4thSpace



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!