Swift Function call list incorrect parameter type

后端 未结 1 1859
醉话见心
醉话见心 2021-01-22 12:35

I define the below list swift class, and try to call the sfAuthenticateUser from the viewcontroller. But the Xcode intellisense list the wrong parameter type other than the type

相关标签:
1条回答
  • 2021-01-22 13:30

    The problem is that you try to call an instance function without having an actual instance at hand.

    You either have to create an instance and call the method on that instance:

    let instance = APISFAuthentication(...)
    instance. sfAuthenticateUser(...)
    

    or define the function as a class function:

    class func sfAuthenticateUser(userEmail: String) -> Bool {
        ...
    }
    

    Explanation:

    What Xcode offers you and what confuses you is the class offers the capability to get a reference to some of its instance functions by passing an instance to it:

    class ABC {
        func bla() -> String {
            return ""
        }
    }
    
    let instance = ABC()
    let k = ABC.bla(instance) // k is of type () -> String
    

    k now is the function bla. You can now call k via k() etc.

    0 讨论(0)
提交回复
热议问题