访问中检索到的管理对象数据与核心数据(Accessing data in retrieved Man

2019-10-22 19:49发布

我试图在检索到的管理对象来访问数据。 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

}

Answer 1:

随着马丁的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!"

        }

    }

如果你想获得最后一个记录,unstead结果[0]使用效果[(results.count)-1]。 希望这将有助于有人在未来:)



文章来源: Accessing data in retrieved Managed Object with Core Data