2016-08-01 2 views
-1

Alamofire lib로 요청하고 있는데 Login, Register ...와 같은 요청을위한 클래스가 있습니다. 따라서 Alamofire가 응답을받을 때 알아야 할 인터페이스가 필요합니다. 신속하게 어떻게 할 수 있습니까?다른 클래스의 응답을 얻기 위해 인터페이스를 사용하십시오.

이 내 Trans.swift

class Trans{ 
func getToken(username: String , password: String){ 
    Alamofire.request(.GET, "http://www.shafadoc.ir/api/DocApp/Token?value=" + username + ":" + password ,parameters:nil) 
     .responseJSON{ response in 
      if let json = response.result.value{ 
      } 
    } 
} 
} 

내 LoginViewController의 일부입니다

if !login_password.text!.isEmpty || !login_username.text!.isEmpty 
    { 
     var trans : Trans = Trans 
     trans.getToken(login_username.text!, password: login_password.text!) 
    } 

답변

2

.responseJSON 방법에 전달 된 블록 요청이 완료 될 때 통지하는 곳입니다. trans.getToken에 콜백 블록을 전달하고 .responseJSON 블록을 호출 할 수 있습니다. 이처럼 :

은 뷰 컨트롤러에서
func getToken(username: String , password: String, completion: Void -> Void){ 
    Alamofire.request(.GET, "http://www.shafadoc.ir/api/DocApp/Token?value=" + username + ":" + password ,parameters:nil) 
     .responseJSON{ response in 
      if let json = response.result.value{ 
      } 
      //... do anything you want with the result, and finally: 
      completion() // <-- call the block 
    } 
} 

:

if !login_password.text!.isEmpty || !login_username.text!.isEmpty 
{ 
    var trans : Trans = Trans 
    trans.getToken(login_username.text!, password: login_password.text!) { 
     //... do your UI stuff 
    } 
} 
+0

당신은 그것에 대해 예를 게시 할 수 있습니까? 내가 로그인 메소드에서 내 요청을 만들고있다. LoginViewController와 요청은 Trans.swift에있다. –

+0

먼저 기존 코드를 게시해라. – Fujia

+0

내 UI 컨트롤러에서 어떻게 응답을받을 수 있습니까? –

관련 문제