2015-10-15 126 views
1

我試圖做一個簡單的debuger在視圖中..我有以下幾點: enter image description here無法轉換字符串...爲字符串斯威夫特

我使用Alamofire發出請求,用responseString接收數據,然後用App.log(response)debugViewControllerlog發送數據,正如你所看到的那樣,這個方法也需要一個字符串。

現在,試圖編譯這個返回的錯誤對我來說很奇怪,因爲我是Swift新手。無法將字符串轉換爲debugViewController.log()中預期的參數類型,實際上它也是String

請讓我在這一個。

在這裏,你有debugViewController

import UIKit 

class debugViewController: UIViewController { 

    @IBOutlet weak var debugTextField: UITextView! 

    @IBAction func dismissDebug(sender: AnyObject) { 
     self.dismissViewControllerAnimated(true, completion: nil) 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func log(data: String) { 
     debugTextField.text = data 
    } 

} 

在這裏,你可以看到我是如何撥打電話和發送數據:

Alamofire.request(.POST, API_URL, parameters: [ "action": "authenticate", "email": userEmail!, "password": userPassword! ]) 
      .responseString { response in 

       guard let value = response.result.value else 
       { 
        return App.alert("error", message: "Did not receive data") 
       } 

       guard response.result.error == nil else 
       { 
        print(response.result.error) 
        return App.alert("error", message: "An error occurred") 
       } 

       App.log (value) 


     } 
+2

如何debugViewController定義了它?看起來你正在調用類本身的實例方法 – giorashc

+0

@giorashc更新了我的問題。 – Eduard

+0

你可以發佈代碼分配數據的地方嗎?另外,檢查數據是否不是可選的。如果是,然後嘗試解開它像數據! –

回答

3

debugViewController是一個類(我建議你開始的類名用大寫字母),並且您試圖在類本身上調用一個實例方法,因此錯誤(因爲它實際上預計類型爲debugViewController的實例)。

你應該保持debugViewController的一個實例被創建,所以你可以調用實例,而不是在類中的日誌方法

+0

好吧,從外行的角度來說,我做錯了什麼是我正在調用非實例化類的動態方法嗎?我的意思是,我沒有Swift背景,但我知道一些OOP。關於camelCase的東西,我剛剛讀過使用camelCase模式更好,但是,好像我錯了。無論如何,爲什麼如果我將func更改爲靜態,我無法訪問日誌方法中的'debugTextField'? – Eduard

+0

是@giorashc是對的。您正在調用debugViewController的實例方法。您可以創建實例或將方法日誌方法作爲類方法。 – iamyogish

+1

@edduvs您無法訪問log方法中的debugTextField,因爲它是debugViewController類的屬性。要訪問一個類的屬性,你需要創建一個實例。 – iamyogish