CoreData - 如何从UITableView获取对象id并根据对象id删除对象?

时间:2015-02-10 02:23:09

标签: ios uitableview swift core-data

现在我遇到了这个问题,我不知道如何在用户从中选择并将其传递到另一个视图控制器后从UITableView获取对象ID,我想根据我已经获取的对象ID删除该对象从前一个。所有数据都来自user实体。

的UITableView

var myList: Array <AnyObject> = []

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
        let context: NSManagedObjectContext = appDel.managedObjectContext!

        /* grab object id here from core data object and pass to NSUserDefault*/
        NSUserDefaults.standardUserDefaults().setObject(objectId, forKey: "userId")

        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

        let deleteViewController = storyBoard.instantiateViewControllerWithIdentifier("accessView") as DeleteViewController
        self.presentViewController(deleteViewController, animated:true, completion:nil)

    }

DeleteViewController

 let objectId = NSUserDefaults.standardUserDefaults().stringForKey("userId")
/* Grab object id from NSUserDefault and delete object here*/

请指教。谢谢。

2 个答案:

答案 0 :(得分:0)

我通常使用两个函数执行此操作,一个用于获取特定用户,另一个用于删除用户。现在这些方法假设每个对象都有一个唯一的id,所以使用以下函数只需调用deleteUser(fetchUser(someID))

func fetchUser(id: Int) -> User? {

    // Define fetch request/predicate
    var fetchRequest = NSFetchRequest(entityName: "User")
    let predicate = NSPredicate(format: "id == \(id)")

    // Assign fetch request properties
    fetchRequest.predicate = predicate
    fetchRequest.fetchBatchSize = 1
    fetchRequest.fetchLimit = 1

    // Handle results
    let fetchedResults = managedObjectContext?.executeFetchRequest(fetchRequest, error: nil)

    if fetchedResults?.count != 0 {

        if let fetchedUser: User = fetchedResults![0] as? User {
            return fetchedUser
        }
    }
    return nil
}

// Delete
func deleteUser(id: Int) {
    if let user = fetchEvent(id) {
        managedObjectContext?.deleteObject(user)
    }
}

答案 1 :(得分:0)

我已经找到了答案。

的UITableView

var myList: Array <AnyObject> = []

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let data: NSManagedObject = myList[indexPath.row] as NSManagedObject

        let userId = data.valueForKeyPath("userId") as? String
        NSUserDefaults.standardUserDefaults().setObject(objectId, forKey: "userId")

        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

        let deleteViewController = storyBoard.instantiateViewControllerWithIdentifier("accessView") as DeleteViewController
        self.presentViewController(deleteViewController, animated:true, completion:nil)

    }

DeleteViewController

let userId = NSUserDefaults.standardUserDefaults().stringForKey("userId")

                    //reference to our app delegate
                    let appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
                    let context: NSManagedObjectContext = appDel.managedObjectContext!

                    let request = NSFetchRequest(entityName: "EntityOne")
                    request.includesSubentities = false
                    request.returnsObjectsAsFaults = false
                    request.predicate = NSPredicate(format: "userId = %@", userId!)
                    let items = context.executeFetchRequest(request, error: nil)!

                    for item in items {
                        context.deleteObject(item as NSManagedObject)
                    }