在我的应用程序中,我试图为api调用创建不同的类。比如点击登录按钮并调用不同类的方法。但是当我想从另一个班级去另一个视图控制器时它会崩溃。
这是我在loginViewController中的代码
let mydata = DataControllerLogin()
mydata.login(txtemail.text!,password: txtPassword.text!)
class DataControllerLogin: UIViewController {
func login(username:String,password:String)
{
if Reachability.isConnectedToNetwork() == true
{
let url = "\(basicURL)login"
let param : [String : AnyObject] = [
"email" : username,
"password" : password
]
Alamofire.request(.POST, url, parameters: param, encoding: .JSON).responseObject(completionHandler: { (response:Response<LoginCode, NSError>) in
if (response.result.value != nil)
{
let LoginCode = response.result.value
let message = LoginCode?.Message
let detail = LoginCode?.result
if (LoginCode?.Status == 1)
{
let controller : LoginViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
self.navigationController?.pushViewController(controller, animated: true)
SVProgressHUD.dismiss()
}
else
{
alertViewShow(self, title: "Sorry", message: message!)
SVProgressHUD.dismiss()
}
if let threedayForecast = LoginCode?.result {
print(threedayForecast.FirstName)
}
}
else
{
}
})
}
else {
alertViewShow(self, title: "No Internet Connection", message: "Make sure your device is connected to the internet.")
}
}
}
但是无论我在哪里定义viewController,它都会在线上崩溃。
let controller : LoginViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
self.navigationController?.pushViewController(controller, animated: true)
显示错误,如
exc_bad_instruction (code=exc_i386_invop subcode=0x0)
所以,如果你知道,请告诉我这是什么问题?
答案 0 :(得分:0)
将此内容写在文件的顶部。
import UIKit
import Alamofire
import SwiftyJSON
typealias SOAPICompletionHandler = (code:Int, error:NSError?, response:NSDictionary?) -> Void
在您的文件中添加以下方法: -
func callApi(strApiName:String, param : [String : AnyObject]?, type:String, header:[String : String]?, completionHandler:SOAPICompletionHandler) {
//let strURL : String = BASEURL+"/"+strApiName
let strURL = strApiName;
if type == POSTREQ {
Alamofire.request(.POST, strURL, parameters: param, encoding: .JSON, headers: header).responseJSON(completionHandler: { (responseData) -> Void in
let isSuccess = JSON(responseData.result.isSuccess)
if isSuccess {
// let swiftyJson = JSON(responseData.result.value! as! NSDictionary) as! AnyObject
completionHandler(code: 1, error: nil, response: responseData.result.value! as? NSDictionary)
} else {
let error = responseData.result.error! as NSError
completionHandler(code: 0, error: error, response: nil)
}
})
} else if type == GETREQ {
Alamofire.request(.GET, strURL, parameters: param, encoding: .JSON, headers: header).responseJSON(completionHandler: { (responseData) -> Void in
let isSuccess = JSON(responseData.result.isSuccess)
if isSuccess {
// let swiftyJson = JSON(responseData.result.value! as! NSDictionary)
completionHandler(code: 1, error: nil, response: responseData.result.value! as? NSDictionary)
} else {
let error = responseData.result.error! as NSError
completionHandler(code: 0, error: error, response: nil)
}
})
} else if type == PUTREQ{
Alamofire.request(.PUT, strURL, parameters: param, encoding: .JSON, headers: header).responseJSON(completionHandler: { (responseData) -> Void in
let isSuccess = JSON(responseData.result.isSuccess)
if isSuccess {
// let swiftyJson = JSON(responseData.result.value! as! NSDictionary)
completionHandler(code: 1, error: nil, response: responseData.result.value! as? NSDictionary)
} else {
let error = responseData.result.error! as NSError
completionHandler(code: 0, error: error, response: nil)
}
})
} else if type == DELETEREQ{
Alamofire.request(.DELETE, strURL, parameters: param, encoding: .JSON, headers: header).responseJSON(completionHandler: { (responseData) -> Void in
let isSuccess = JSON(responseData.result.isSuccess)
if isSuccess {
// let swiftyJson = JSON(responseData.result.value! as! NSDictionary)
completionHandler(code: 1, error: nil, response: responseData.result.value! as? NSDictionary)
} else {
let error = responseData.result.error! as NSError
completionHandler(code: 0, error: error, response: nil)
}
})
}
else if type == PATCHREQ{
Alamofire.request(.PATCH, strURL, parameters: param, encoding: .JSON, headers: header).responseJSON(completionHandler: { (responseData) -> Void in
let isSuccess = JSON(responseData.result.isSuccess)
if isSuccess {
// let swiftyJson = JSON(responseData.result.value! as! NSDictionary)
completionHandler(code: 1, error: nil, response: responseData.result.value! as? NSDictionary)
} else {
let error = responseData.result.error! as NSError
completionHandler(code: 0, error: error, response: nil)
}
})
}
}
你需要添加Alamofire和SwiftyJSON。
希望最好。