2016-01-20 130 views
2

我定義了下面的列表swift類,並嘗試從viewcontroller調用sfAuthenticateUser。但Xcode智能感知列出了除我定義的類型之外的錯誤參數類型。Swift函數調用列表不正確的參數類型

錯誤:無法將類型的值 '串' 預期參數類型 'APISFAuthentication'

的Xcode 7.1版本(7B91b)

// - 視圖 - 控制器方法調用如下

@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; 
    } 
} 

請參閱t o屏幕截圖 enter image description here

回答

3

問題是您嘗試調用實例函數而沒有實際實例。

你要麼必須創建一個實例,並調用該方法在該實例:

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

或定義函數作爲一類功能:

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

說明:

Xcode爲您提供了什麼以及讓您感到困惑的是,該類提供了通過pa獲得對其某些實例函數的引用的功能ssing一個實例吧:

class ABC { 
    func bla() -> String { 
     return "" 
    } 
} 

let instance = ABC() 
let k = ABC.bla(instance) // k is of type() -> String 

k現在功能bla。您現在可以通過k()致電k等。

相關問題