使用核心数据访问检索到的托管对象中的数据
Accessing data in retrieved Managed Object with Core Data
我正在尝试访问检索到的托管对象中的数据。 Xcode 给我错误 "Cannot subscript a value type "[AnyObject]?",索引类型为 'int'"。有谁知道我应该怎么做?谢谢
这是我保存数据的方法,它是从我的 viewDidLoad 方法调用的:
// save data
func saveData (jmeno:String, adresa:String, telefon:String) {
// delegate
let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
// managed object context
let managedObjectContext = delegate.managedObjectContext
// entity description
let entityDescription = NSEntityDescription.entityForName("Contacts", inManagedObjectContext: managedObjectContext!)
// instance
let contact = Contacts(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext!)
// ukládání do attributů
contact.name = jmeno
contact.adress = adresa
contact.phone = telefon
// ukládání do cd
var error:NSError?
managedObjectContext?.save(&error)
if let err = error {
println("error")
}
println("neco")
}
最后是我的方法,它应该从核心数据中给我数据。此方法也是从 viewDidLoad 方法调用的:
func giveMeData () {
//
// FETCHING
// delegate
let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
// managed object context
let managedObjectContext = delegate.managedObjectContext!
// entity description
let entityDescription = NSEntityDescription.entityForName("Contacts", inManagedObjectContext: managedObjectContext)
// request
let request = NSFetchRequest()
request.entity = entityDescription
// uložení výsledku fetche do array
var results = managedObjectContext.executeFetchRequest(request, error: nil)
//
// GETTING
let match = results[0] as! NSManagedObject // HERE IS ERROR
let nameString = match.valueForKey("name") as! String
let addressString = match.valueForKey("address") as! String
let phoneString = match.valueForKey("phone") as! String
}
在 Martin R 的帮助下,我终于解决了我的问题。这是我的 giveMeData 方法代码:
// entity descritption
let entityDescription = NSEntityDescription.entityForName("Contact", inManagedObjectContext: managedObjectContext!)
// request
let request = NSFetchRequest(entityName: "Contacts")
//request.entity = entityDescription
// predicate - stanovení parametrů, na základě kterejch to vyhodí výsledek
//let pred = NSPredicate(format: "(name: %@)", TFname.text)
let pred = NSPredicate(format: "name contains[c] %@", TFname.text)
request.predicate = pred
var error:NSError?
var objects = managedObjectContext?.executeFetchRequest(request, error: &error)
// pokud objects obsahuje data (tedy data existují a uložiště není nil) tak:
if let results = objects {
if results.count > 0 {
let match = results[0] as! NSManagedObject
self.TFname.text = match.valueForKey("name") as! String
self.TFadress.text = match.valueForKey("adress") as! String
self.TFphone.text = match.valueForKey("phone") as! String
self.label.text = "Matches found!"
} else {
self.label.text = "No match!"
}
}
如果要获取最后一条记录,请使用results[(results.count)-1]代替results[0]。希望对以后的人有所帮助:)
我正在尝试访问检索到的托管对象中的数据。 Xcode 给我错误 "Cannot subscript a value type "[AnyObject]?",索引类型为 'int'"。有谁知道我应该怎么做?谢谢
这是我保存数据的方法,它是从我的 viewDidLoad 方法调用的:
// save data
func saveData (jmeno:String, adresa:String, telefon:String) {
// delegate
let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
// managed object context
let managedObjectContext = delegate.managedObjectContext
// entity description
let entityDescription = NSEntityDescription.entityForName("Contacts", inManagedObjectContext: managedObjectContext!)
// instance
let contact = Contacts(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext!)
// ukládání do attributů
contact.name = jmeno
contact.adress = adresa
contact.phone = telefon
// ukládání do cd
var error:NSError?
managedObjectContext?.save(&error)
if let err = error {
println("error")
}
println("neco")
}
最后是我的方法,它应该从核心数据中给我数据。此方法也是从 viewDidLoad 方法调用的:
func giveMeData () {
//
// FETCHING
// delegate
let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
// managed object context
let managedObjectContext = delegate.managedObjectContext!
// entity description
let entityDescription = NSEntityDescription.entityForName("Contacts", inManagedObjectContext: managedObjectContext)
// request
let request = NSFetchRequest()
request.entity = entityDescription
// uložení výsledku fetche do array
var results = managedObjectContext.executeFetchRequest(request, error: nil)
//
// GETTING
let match = results[0] as! NSManagedObject // HERE IS ERROR
let nameString = match.valueForKey("name") as! String
let addressString = match.valueForKey("address") as! String
let phoneString = match.valueForKey("phone") as! String
}
在 Martin R 的帮助下,我终于解决了我的问题。这是我的 giveMeData 方法代码:
// entity descritption
let entityDescription = NSEntityDescription.entityForName("Contact", inManagedObjectContext: managedObjectContext!)
// request
let request = NSFetchRequest(entityName: "Contacts")
//request.entity = entityDescription
// predicate - stanovení parametrů, na základě kterejch to vyhodí výsledek
//let pred = NSPredicate(format: "(name: %@)", TFname.text)
let pred = NSPredicate(format: "name contains[c] %@", TFname.text)
request.predicate = pred
var error:NSError?
var objects = managedObjectContext?.executeFetchRequest(request, error: &error)
// pokud objects obsahuje data (tedy data existují a uložiště není nil) tak:
if let results = objects {
if results.count > 0 {
let match = results[0] as! NSManagedObject
self.TFname.text = match.valueForKey("name") as! String
self.TFadress.text = match.valueForKey("adress") as! String
self.TFphone.text = match.valueForKey("phone") as! String
self.label.text = "Matches found!"
} else {
self.label.text = "No match!"
}
}
如果要获取最后一条记录,请使用results[(results.count)-1]代替results[0]。希望对以后的人有所帮助:)