从核心数据fetch swift 3打印数组元素

时间:2017-07-10 16:05:45

标签: ios core-data swift3

我想从我获取的数组中获取属性。但是,当我尝试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")
    }





}

0 个答案:

没有答案