我試圖做一個簡單的debuger在視圖中..我有以下幾點: 無法轉換字符串...爲字符串斯威夫特
我使用Alamofire發出請求,用responseString
接收數據,然後用App.log(response)
向debugViewController
的log
發送數據,正如你所看到的那樣,這個方法也需要一個字符串。
現在,試圖編譯這個返回的錯誤對我來說很奇怪,因爲我是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)
}
如何debugViewController定義了它?看起來你正在調用類本身的實例方法 – giorashc
@giorashc更新了我的問題。 – Eduard
你可以發佈代碼分配數據的地方嗎?另外,檢查數據是否不是可選的。如果是,然後嘗試解開它像數據! –