我想从我获取的数组中获取属性。但是,当我尝试index out of range
之类的内容时,我不断收到txt_name.text = user[1].name!
错误。 admin_info
是一个全局变量,包含来自另一个视图控制器的数据。
var admin_info = String()
var user:[Users] = []
@IBOutlet weak var txt_name: UITextField!
@IBOutlet weak var txt_password: UITextField!
@IBOutlet weak var txt_email: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let query:NSFetchRequest<Users> = Users.fetchRequest()
let key = admin_info
let predicate = NSPredicate(format: "name matches[c] %@", key)
query.predicate = predicate
do{
user = try context.fetch(query)
}catch{
print("error")
}
}