首页 > 解决方案 > 我只想通过比较基本上通过webapi从数据库获得的角色名称来移动到另一个屏幕......有什么办法吗?

问题描述

这是登录按钮操作代码......我真正想要的是如果我点击登录按钮,如果它的(代理)<--(它的 r_name)那么它应该移动到下一个屏幕......响应变量包含来自数据库的所有数据我只想从该响应变量中获取 r_name 就是这样

@IBAction func btnLogin(_ sender: Any) {

//normally it moves to the next screen by applying below code but I want to compare roles name if e.g role name="agent" then move to another screen
        
         let response = mm.alluser(phoneNo: txtPhoneNumber.text!, Password: txtPassword.text!)
        if response.count ?? 0 > 0
        {                         
            let next : tabViewController = self.storyboard?.instantiateViewController(withIdentifier: "tabViewController") as! tabViewController
             self.navigationController?.pushViewController(next, animated: true)
           showalertforloginsuccess()
            print("Login Successfull")
        }else
        {
            showalertforloginfail()
            print("Not Login")
        }
    }

//这里是一个包含角色属性的类userModel


public class UserModel:Codable
{
    
    public var u_id: Int = 0
    public var u_name: String?=""
    public var phoneNumber: String?=""
    public var address: String? = ""
    public var password: String? = ""
    public var r_id: Int? = 0
    public var r_name: String? = ""
    
}

//用户管理器类通过名为(alluser)的函数与web api交互

class Usersmanager
{
    
    var apiwrapper=APIWrapper()
    var decoder=JSONDecoder()
    var encoder=JSONEncoder()
    var Message=""
    
    public func alluser(phoneNo: String, Password : String)->[UserModel]
    {
        var users:[UserModel]=[]
        let result=apiwrapper.getMethodCall(controllerName: "user", actionName: "Userlogin?phoneNo=\(phoneNo)&Password=\(Password)")
      
        if result.ResponseCode==200{
            
            guard let data=result.ResponseData
                else{
                    
                    Message=result.ResponseMessage
                    return users
            }
            //data is now valid
            users = try! decoder.decode([UserModel].self,from: data)
        }
        else {
            
            Message=result.ResponseMessage
        }
        return users
    }  
}

标签: swiftwebapi

解决方案


推荐阅读