使用 Core Data 访问检索到的托管对象中的数据

Accessing data in retrieved Managed Object with Core Data(使用 Core Data 访问检索到的托管对象中的数据)
本文介绍了使用 Core Data 访问检索到的托管对象中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问检索到的托管对象中的数据.Xcode 给我错误无法下标值类型[AnyObject]?",索引类型为int".有人知道我应该怎么做吗?谢谢

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

这是我保存数据的方法,从我的 viewDidLoad 方法中调用:

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

}

最后是我的方法,它应该从核心数据中给我数据.这个方法也是从 viewDidLoad 方法中调用的:

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

}

推荐答案

在 Martin R 的帮助下,我终于解决了我的问题.这是我的 giveMeData 方法的代码:

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

        }

    }

如果要获取最后一条记录,请使用 results[(results.count)-1] 代替 results[0].希望它对将来的某人有所帮助:)

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:)

这篇关于使用 Core Data 访问检索到的托管对象中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)