Accessing data in retrieved Managed Object with Co

2019-08-13 23:42发布

问题:

I'm trying to access a data in the retrieved Managed Object. Xcode giving me error "Cannot subscript a value type "[AnyObject]?" with an index of type 'int'". Doesn anybody know what I should do to? Thank you

This is my method which saves data and which is called from my viewDidLoad method:

// 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")

}

And finally there is my method which is supposed to give me data from core data. Also this method is called from viewDidLoad method:

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

}

回答1:

With Martin R's help I finally solved my problem. Here is my code from giveMeData method:

// 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!"

        }

    }

If you want to get last record, unstead of results[0] use results[(results.count)-1]. Hope that it will help to someone in future:)