ios - 我需要接受 alamofire 关闭的响应并将其发送到另一个班级
问题描述
我需要接受 alamofire 关闭的响应并将其发送到另一个班级。我在闭包内得到响应,但是当我尝试发送到另一个类时,我得到:“致命错误:在展开可选值时意外发现 nil ”
我需要从闭包中获取响应并将其设置在另一个类的 IBOutlet 中。
此外,我尝试在全局变量中设置关闭响应,但我得到了同样的错误。
class DrawInformationViewController: UIViewController {
@IBOutlet weak var nameLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
}
class RequestViewController: UIViewController{
var getName: String?
func testAlamofire(){
if let JSON = response.result.value{
self.jsonArray = JSON as? NSArray
//this for doesn't matter it is just for an example, to get the idea
for item in self.jsonArray! as! [NSDictionary]{
let name = item["name"] as? String
print(name) //until here everything is ok
//the problem is when I try to send this value
DrawInformationViewController().nameLabel.text = name //here is when it get the error: "Fatal error: Unexpectedly found nil while unwrapping an Optional value"
//Also I try to set the value in a global var
getName = name //There is not problem
}
}
}
}
func setValueFromClosure(){
DrawInformationViewController().nameLabel.text = getName
//Here I get the same error: "Fatal error: Unexpectedly found nil while unwrapping an Optional value"
}
}
解决方案
尝试按如下方式初始化 getName:
变量 getName: 字符串 = ""
干杯。
推荐阅读
- java - 我可以使用 Springs JpaRepository 检索已使用 DBAppender 附加的 Logback LoggingEvent 记录吗
- laravel - Laravel 通过方法获取路由
- angular - Angular2- 带有下拉 NGbDropDown 或简单 Select Dropdown 的虚拟滚动条
- cmake - CMake ExternalProject_Add 使用自动设置变量
- testng - 在 TestNG 中使用 DependsOnMethod 时出错
- security - 使用 jwt 和 2 个服务器作为 secret_key 股东的跨域身份验证
- javascript - 如何处理用户在 Actions on Google 中提供的响应
- react-native - React Native __NSOperationInternal 崩溃报告
- vba - 如何在 Workbook.Close 方法之后运行存储在工作簿模块中的宏?
- java - 隔行扫描 BufferedWriter 和 PrintWriter