Ios 无法转换字符串。。。迅速插队
我试图在视图中制作一个简单的调试程序。。我有以下几点: 我正在使用Alamofire发出请求,使用Ios 无法转换字符串。。。迅速插队,ios,swift,swift2,Ios,Swift,Swift2,我试图在视图中制作一个简单的调试程序。。我有以下几点: 我正在使用Alamofire发出请求,使用responseString接收数据,然后使用App.log(response)将其发送到debugViewController的log方法,正如您所看到的,该方法也需要字符串 现在,尝试编译它会返回一个错误,这对我来说非常奇怪,因为我是Swift新手。无法将字符串转换为debugViewController.log()中所需的参数类型,实际上也是string 请告诉我这件事 这里有debugVi
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
是一个类(我建议您以大写字母开头类名),您试图调用类本身的实例方法,因此出现错误(因为它实际上需要类型为debugViewController
的实例)
创建
debugViewController
后,您应该保留它的一个实例,以便可以在实例上而不是在类上调用log方法debugViewController是如何定义的?看起来您正在类上调用一个实例方法itself@giorashc更新了我的问题。你能把代码贴在数据赋值的地方吗?另外,检查数据是否不是可选的。如果是,请尝试将其像数据一样展开!如果使用的是response.result,则它的类型为anyobject。你把它打成字符串了吗。在将其发送到App.log()?@iamygish时,我已经用响应部分更新了我的问题。好吧,用外行的话说,我做错了的是,我在非实例化类上调用了动态方法,对吗?我的意思是,我没有Swift的背景,但我知道一些OOP。关于camelCase的内容,我刚刚读到使用camelCase模式更好,但是,似乎我弄错了。无论如何,为什么如果我将func更改为static,我就不能再访问log方法中的debugTextField
?是的@giorashc是正确的。您正在调用debugViewController的实例方法。您可以创建一个实例,也可以将方法日志方法设置为类方法。@edduvs您不能访问日志方法中的debugTextField,因为它是debugViewController类的属性。要访问类的属性,您需要创建一个实例。@iamygish如果我使用log as class方法,我将无法再从IBOutlet(debugTextField)访问该值。现在,它说,在展开可选值时意外地发现了nil,即使字符串包含我的JSON响应。该怎么办?@edduvs是当您调用类函数时,您将无法访问IBOutlets,因为outlets与实例关联。现在“在展开可选值时意外地发现了nil”,这是因为您正在尝试设置作为输出的textfield的文本。textfield为nil,因为您尚未创建该类的实例。