title: Accessing variables in protocol methods in swift
body: <p>can anyone please help me with this swift code, i'm a swift beginner.
  i'm trying to create a login page using MVC and it looks like this <a href="http://i.stack.imgur.com/VhAtk.png" rel="nofollow">enter image description here</a></p>
  
  <p>so when the user enters their login information and the status from the JSON data is "s" for a successful login it should display some test information, and when the login status is "f" for a failed login it should stay in the same login page, i'm trying to access the status in the protocol method in the code below:</p>
  
  <p>so whenever the user presses the button it should take the required action but the status in the code below is nil so whenever i run the application the information is displayed wether the login was successful or not</p>
  
  <pre><code>//this piece of code is in the LoginViewController
          func didReceivedLoginInfo(info : [LoginModel]) -&gt; String
              {
                  self.loginData = info
                  print("didReceivedLOOGIN count is \(loginData!.count)")//the print is successful here
                  for i in 0..&lt;loginData!.count
                  {
                      print("LoginData Info : \(loginData![i])")//the print is successful here
                  }
                  statuss = loginData![0].status
                  print("HERE YOU GO, THIS IS WHAT I FOUND: \(statuss!)")//the print is successful here
                 return statuss! 
              }
  
  //this piece of code is in the LoginViewController
      func buttonAction(sender:UIButton!)
          {
              var btnsendtag: UIButton = sender
                  var uName  = username!.text as? String!
                  var uPass  = password!.text as? String!
                  parser = LoginParser()
                  parser!.getLoginInfo(uName!, pass: uPass!)
                  var status = loginData?[0].status
                  print("from View Controller this is the msg: \(status)")
                  parser!.delegate = self
                  if status == "s"            {
                      print("from View Controller this is the status: \(status)")
                      let vc = ViewController()
                      let navigationController = UINavigationController(rootViewController: vc)
                      self.presentViewController(navigationController, animated: true, completion: nil)
                  }
  
                  else {
                      let vc = LoginViewController()
                      let navigationController = UINavigationController(rootViewController: vc)
                      self.presentViewController(navigationController, animated: true, completion: nil)
                             }
          }
  
  
  
       ////this piece of code is in the LoginParser
          import UIKit
  
          @objc protocol LoginParserDelegate : NSObjectProtocol
  
          {
  
              func didReceivedLoginInfo (info : [LoginModel]) -&gt; String
  
              optional func didRecivedError()
  
          }
  
  
  
          class LoginParser: NSObject {
  
              // var  webData: NSMutableData?
  
              var webData: NSData?
              weak var delegate : LoginParserDelegate?
              var name = String ()
              var model : LoginModel?
              var alertView:UIAlertView?
              var MainLogin : [LoginModel] = []
  
              func getLoginInfo (usern : String, pass : String){
  
                  var post:NSString = "LID=\(usern)&amp;PWD=\(pass)&amp;Passcode=s@v#"
                  var url:NSURL = NSURL(string: "some link")!
                  var postData:NSData = post.dataUsingEncoding(NSUTF8StringEncoding)!
                  var postLength:NSString = String( postData.length )
  
                  var request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
  
                  request.HTTPMethod = "POST"
  
                  request.HTTPBody = postData
  
                  request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
  
                  request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
  
                  request.setValue("application/json", forHTTPHeaderField: "Accept")
  
                  var response: NSURLResponse?
  
                  let connection = NSURLConnection(request: request, delegate: self)
              }
  
  
              func connection(connection: NSURLConnection, didFailWithError error: NSError){
  
                  if delegate != nil {
                      if delegate!.respondsToSelector(Selector("didRecivedLoginInfo")){
                          delegate!.didRecivedError!()
                      }
  
                  }
              }
  
              func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse){
  
                  webData = NSData()
  
  
              }
  
              func connection(connection: NSURLConnection, didReceiveData data: NSData){
  
                  webData! = data
              }
  
              func connectionDidFinishLoading(connection: NSURLConnection){
                  let responseString = NSString(data: webData!, encoding: NSUTF8StringEncoding)
                  print("responseString \(responseString!)")
  
                  processUserTypes ()
  
              }
  
              func processUserTypes ()-&gt;[LoginModel]
              {
                  model = LoginModel()
  
                  // Process JSON
  
                  do {
                      let result = try NSJSONSerialization.JSONObjectWithData(webData!, options: NSJSONReadingOptions.MutableContainers)as? NSDictionary
  
  
                      var Login = LoginModel()
  
  
                      let status = result?.objectForKey("Status") as? String
                      let msg = result?.objectForKey("En") as? String
                      let employeename=result?.objectForKey("DisplayName")as? String
                      print("STATUS IN PARSER IS \(status!)")
  
                      if status == "s"
  
                      {
  
                          print(status!)
                          Login.name = employeename
                          Login.status = status
                          Login.message = msg
                          alertView = UIAlertView()
                          alertView!.title = "Login successful"
                          alertView!.message = "Welcome \(employeename!)"
                          alertView!.delegate = self
                          alertView!.addButtonWithTitle("OK")
                          alertView!.show()
                          MainLogin.append(Login)
                          print("count in login is  \(MainLogin.count)")
                          if delegate != nil
                          {
                              delegate!.didReceivedLoginInfo(MainLogin)
                          }
  
                      }
  
                      else
  
                      {
                          Login.message = msg
                          alertView = UIAlertView()
                          alertView!.title = "Login failed"
                          alertView!.message = msg!
                          alertView!.delegate = self
                          alertView!.addButtonWithTitle("OK")
                          alertView!.show()
  
                          MainLogin.append(Login)
                          print("this is the main login\(MainLogin)")
  
  
                      }
  
                             }
                  catch {
  
                      if delegate != nil {
                          if delegate!.respondsToSelector(Selector("didRecivedLoginInfo")){
                              delegate!.didRecivedError!()
                          }
  
  
                      }
  
  
  
                  }
                  return MainLogin
              }
          }
  </code></pre>
  
  <p>can anyone help me with this?
  thanks</p>