自8月以来,我一直在学习iOS开发,而我现在才开始开发我的第一个应用程序。在我甚至不知道如何开发应用程序之前,我一直在创建具有MINIMAL功能的小型原型应用程序,只是为了了解如何做我的实际产品的一部分。现在,我停留在一个仅创建一个帐户并将其存储在数组中以供以后使用的项目中,即在“我的个人资料”页面上显示数据模型对象及其各种属性;
更具体地说,我只是试图了解如何(从最基本的“基本知识”层面)从用户那里获取输入的信息(用户名,密码,全名),将其转换为我的数据模型对象,然后将其存储将对象分成相同类型的对象数组,最后在配置文件页面上显示数据模型对象。我正在使用它来了解应用程序一般体系结构的一部分。
import Foundation
import UIKit
class Account {
var username: String
var password: String
var fullName: String
static var accounts: [Account] = []
init(username: String, password: String, fullName: String) {
self.username = username
self.password = password
self.fullName = fullName
}
}
我的数据模型文件Account.swift
import UIKit
class RegisterViewController: UIViewController {
@IBOutlet var usernameField: UITextField!
@IBOutlet var passwordField: UITextField!
@IBOutlet var fullNameField: UITextField!
var newAccount = Account(username: "", password: "", fullName: "")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func createAccount(_ sender: UIButton) {
newAccount.username = usernameField.text!
newAccount.password = passwordField.text!
newAccount.fullName = fullNameField.text!
Account.accounts.append(newAccount)
}
我的视图控制器的注册视图
import UIKit
class ViewController: RegisterViewController {
@IBOutlet var usernameLogin: UITextField!
@IBOutlet var passwordLogin: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func loginToProfile(_ sender: UIButton) {
if usernameLogin.text! == newAccount.username {
print("success")
} else {
print("error")
}
if passwordLogin.text! == newAccount.password {
print("success")
} else {
print("error")
}
}
@IBAction func unwindToLogin(unwindSegue: UIStoryboardSegue) {
}
}
我的登录视图的veiw控制器。
顶部是创建帐户的视图,底部是登录视图。我现在的问题是通过登录vc中的if语句,我的控制台输出错误,表明在我尝试注册帐户然后登录后字符串不匹配。
I am aware that I force-unwrapped all the uitextfield optionals didnt think that affected much in this specific case. Nonetheless if there are any other blaring issues feel free to point them out
From what it looks, you have 2 different controllers: RegisterViewController
and another ViewController
, which, although it subclasses RegisterViewController
, is not the same instance. So when you fill in newAccount
in RegisterViewController
, it doesn't mean you are filling in newAccount
inside ViewController
.
What you should do, is instead to look for matching account in Account.accounts
, and compare its properties to the one provided by a user, i.e.:
@IBAction func loginToProfile(_ sender: UIButton) {
// Find the right account
guard let account = Account.accounts.filter { $0.username == usernameLogin.text! } else {
print("Error: No such account")
return
}
// Now check if passwords match
guard account.password == passwordLogin.text! else {
print("Error: incorrect password")
return
}
Note: this is just minimal change to get you unstuck. But really, I would do some other changes:
There's no reason for ViewController
to subclass RegisterViewController
- they are different view controllers, they share information, but they are completely different in their functional purpose.
I would make Account
a struct
, that represents a single account. While static var accounts: [Account] = []
should be in some other class, that holds all the accounts, and, in the future, retrieves them from elsewhere. Simplest form of MVC pattern or an Accounts
singleton for now will do. Later you can improve on that part, while struct Account
will probably remain unchanged.
Second change would also allow you to change the way you compare accounts: instead of function above, you could do something like (many variations possible, giving just 1 example, which assumes you have a singleton which holds all accounts)
@IBAction func loginToProfile(_ sender: UIButton) {
Accounts.shared.login(username: usernameLogin.text!, password: passwordLogin.text!)
这将使您完全抽象如何从视图控制器验证用户
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句