ios - 无法将字符串...转换为 Swift 中的字符串

标签 ios swift swift2

我正在尝试在 View 中创建一个简单的调试器..并且我有以下内容: enter image description here

我使用 Alamofire 发出请求,使用 responseString 接收数据,然后使用 App.log( response )debugViewController 发送数据如您所见,code> 的 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 是一个类(我建议您以大写字母开头类名),并且您试图调用类本身的实例方法,因此会出现错误(因为它实际上期望一个debugViewController 类型的实例)。

您应该在创建 debugViewController 实例后保留它,以便您可以在实例上而不是在类上调用 log 方法

关于ios - 无法将字符串...转换为 Swift 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33148733/

相关文章:

ios - 如何确定 NSDate 是否是今天?

ios - Webview 和导航 Controller

swift - 如何将字符串拆分为 [String] 而不是 [Substring]?

swift - 为什么没有为可选数组定义 Equatable

ios - 如何快速显示来自 UIPreviewAction 的 View Controller

facebook - “InvalidOperationException”,原因 : 'fbauth2 is missing from your Info.plist under LSApplicationQueriesSchemes and is required for iOS 9.0'

ios - 导航/TabBarController消失

ios - 使用 Firebase 的 Google 注销按钮

ios - 强制WKWebView显示移动版本

ios - 如何将 UIView 添加到 SCNPlane ARKit