我试图在检索到的管理对象来访问数据。 Xcode中给我错误“无法下标值类型‘[AnyObject]?’类型‘诠释’的指标”。 隐而不宣有人知道我应该做的? 谢谢
这是我的方法,节省了数据和从我的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
}