类型的价值' [用户]'没有会员'用户名'

时间:2018-03-24 15:45:21

标签: swift compiler-errors

我正在尝试创建一个简单的用户登录ViewController,它连接到一个充满我的应用程序用户信息的数据库,但是我收到了标题中声明的错误。

import UIKit
import Alamofire

class Users: Decodable {
    let username: String
    let email: String
    let password: String

    init(username: String, email: String, password: String) {
        self.username = username
        self.email = email
        self.password = password
}


class LoginVC: UIViewController {

    var loggingin = [Users]()

    @IBOutlet weak var usernameTxtField: UITextField!

    @IBOutlet weak var passwordTxtField: UITextField!

    @IBOutlet weak var checkCredsBtn: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let jsonURL = "http://host-2:8888/getLogin.php"
        let url = URL(string: jsonURL)

        URLSession.shared.dataTask(with: url!) { (data, response, error) in

            do {

                self.loggingin = try JSONDecoder().decode([Users].self, from: data!)

                for eachUser in self.loggingin {
                    print(eachUser.username + " : " + eachUser.password)
                }

            }
            catch {
                print("Error")
            }
        }.resume()

    }
    @IBAction func checkCreds(_ sender: Any) {
        if usernameTxtField == loggingin.username && passwordTxtField == loggingin.password {
            print("YES")
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

这个编译时错误的原因出现在G函数中,您试图直接从数组访问checkCredsusername属性,这显然是不正确的。你应该做的是从password数组中获取所需的对象并与其属性进行比较:

loggingin

在上面的例子中,我得到了第一个对象;我假设你已经能够从@IBAction func checkCreds(_ sender: Any) { let currentUser = loggingin[0] if usernameTxtField == currentUser.username && passwordTxtField == currentUser.password { print("YES") } } 获得所需的对象currentUser(使用的索引是什么)。