【问题标题】:Swift Function call list incorrect parameter typeSwift 函数调用列表不正确的参数类型
【发布时间】:2016-01-20 18:21:08
【问题描述】:

我定义了下面的列表 swift 类,并尝试从视图控制器调用 sfAuthenticateUser。但是 Xcode 智能感知列出了错误的参数类型,而不是我定义的类型。

错误:无法将“字符串”类型的值转换为预期的参数类型“APISFAuthentication”

Xcode 版本 7.1 (7B91b)

//ViewController方法调用如下

@IBAction func ActionNext(sender: AnyObject) {
    let sss =  APISFAuthentication.sfAuthenticateUser(<#T##APISFAuthentication#>)
}

// 类定义如下

class APISFAuthentication {
    init(x: Float, y: Float) {            
    }

    func sfAuthenticateUser(userEmail: String) -> Bool {
        let manager = AFHTTPRequestOperationManager()
        let postData = ["grant_type":"password","client_id":APISessionInfo.SF_CLIENT_ID,"client_secret":APISessionInfo.SF_CLIENT_SECRET,"username":APISessionInfo.SF_GUEST_USER,"password":APISessionInfo.SF_GUEST_USER_PASSWORD]

        manager.POST(APISessionInfo.SF_APP_URL,
            parameters: postData,
            success: { (operation, responseObject) in
                print("JSON: " + responseObject.description)
            },
            failure: { (operation, error) in
                print("Error: " + error.localizedDescription)

        })
        return true;
    }
}

请参考屏幕截图

【问题讨论】:

    标签: ios swift swift2 xcode7


    【解决方案1】:

    问题是您试图在手头没有实际实例的情况下调用实例函数。

    您要么必须创建一个实例并调用该实例上的方法:

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

    或将函数定义为类函数:

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

    说明:

    Xcode 为您提供的以及让您感到困惑的是,该类提供了通过将实例传递给它来获取对其某些实例函数的引用的能力:

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

    k 现在是函数bla。您现在可以通过k() 等拨打k。

    【讨论】:

      猜你喜欢
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多