Search code examples
swiftswift3nsuserdefaultsidentifier

use of unresolved identifier 'result' swift 3


I'm writing a login page for my app and i'm getting this error social media app , xcode 8.3.2 , swift 3 i've tried target membership in file inspector and nothing changed also I removed test units (UITest and Test) and renew them , it didn't worked either. at the line 41 I'm getting this error "use of unresolved identifier 'result'"

the picture below explains the code Picture

import UIKit

class LoginViewController : UIViewController
{
    @IBOutlet weak var txt_username: UITextField!
    @IBOutlet weak var txt_password: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    @IBAction func btn_log_in_click(_ sender: Any){
        let server=MultipartUtility (Url:"http://x.x.x.x/appname-api/user/login/")
//I.m hiding the real ip and details for posting this
            server.AddFormField("username", value: txt_username.text)
            server.AddFormField("password", value: txt_password.text)


let task = URLSession.shared.dataTask(with: server.execute())
{Data,URLResponse,error in
    if error != nil{
    print(error as Any)
        return
    }
    do{
        let json = try JSONSerialization.jsonObject(with: Data!, options: .allowFragments)
        if let json_result = json as? [String: Any]{
            let result = json_result ["result"] as? String
            if result == "0"
            {
                DispatchQueue.main.async {
                    let alert = UIAlertController(title:"Incorrect Username",message : "The username you entered doesn't appear to belong to an account. Please check your username and try again", preferredStyle : .alert)
                    let alert_action = UIAlertAction(title: "Try Again", style: .default, handler: nil)
                    alert.addAction(alert_action)
                    self.present(alert, animated: true, completion: nil)
                }
            }
        }
        else{

            DispatchQueue.main.async {
            UserDefaults.standard.set(result!, forKey: "user_id")
//" use of unresolved identifier 'result' "
            let current_view=UIApplication.shared.windows[0] as UIWindow
            let new_view=(self.storyboard? .instantiateViewController(withIdentifier: "tab_bar"))! as UIViewController
            UIView.transition(from: (current_view.rootViewController? .view)!, to:new_view.view , duration: 0.65, options: .transitionFlipFromRight, completion: {(action) in current_view.rootViewController=new_view
            })
            }
        }
    }
    catch{
    }
}
        task.resume()

      }
}

Solution

  • if let json_result = json as? [String: Any]
                    {
                        let result = json_result ["result"] as? String
                        if result == "0"
                        {
                            DispatchQueue.main.async {
                                let alert = UIAlertController(title:"Incorrect Username",message : "The username you entered doesn't appear to belong to an account. Please check your username and try again", preferredStyle : .alert)
                                let alert_action = UIAlertAction(title: "Try Again", style: .default, handler: nil)
                                alert.addAction(alert_action)
                                self.present(alert, animated: true, completion: nil)
                            }
                        }
                        else
                        {
                            DispatchQueue.main.async {
                                UserDefaults.standard.set(result!, forKey: "user_id")
                                //" use of unresolved identifier 'result' "
                                let current_view=UIApplication.shared.windows[0] as UIWindow
                                let new_view=(self.storyboard? .instantiateViewController(withIdentifier: "tab_bar"))! as UIViewController
                                UIView.transition(from: (current_view.rootViewController? .view)!, to:new_view.view , duration: 0.65, options: .transitionFlipFromRight, completion: {(action) in current_view.rootViewController=new_view
                                })
                            }
    
                        }
                    }
                    else{
                    // Error in jsonSerialization
                    }