简体   繁体   中英

Passing an object from one viewcontroller to another viewcontroller got nil in swift

Recently, I'm learning about CoreData in Swift. My purpose is about to send the value of one object "editContact" in class "AllContactTableViewController" as code below.

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

    let appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let managedObjectContext:NSManagedObjectContext = appDelegate.managedObjectContext!
    let entity = NSEntityDescription.entityForName("Contact", inManagedObjectContext: managedObjectContext)
    var request = NSFetchRequest(entityName: "Contact")
    request.returnsObjectsAsFaults = false

    var results: NSArray = managedObjectContext.executeFetchRequest(request, error: nil)!

    let editContactView : EditContactTableViewController = EditContactTableViewController()

    var editContact : Contact = results.objectAtIndex(indexPath.row) as Contact

    editContactView.editContact = editContact

    println("\(editContactView.editContact)")
}

to another viewcontroller called "EditContactTableViewController" (as code below)

    class EditContactTableViewController: UITableViewController {  
    var editContact : Contact!

    override func viewDidLoad() {
        super.viewDidLoad()

        firstNameField.text = self.editContact.firstName
        lastNameField.text = self.editContact.lastName
        phoneField.text = self.editContact.phone
        emailField.text = self.editContact.email
        companyField.text = self.editContact.company
        addressField.text = self.editContact.address

    }
}

then it caused the error as "fatal error: unexpectedly found nil while unwrapping an Optional value"

in the log. It seems the value of object called "editContact" in this class has changed to nil.

Do you have idea how to fix this problem?

First, use a NSFetchedResultsController . There are many advantages, and you also will safely get the object you need with

self.fetchedResultsController.objectAtIndexPath(indexPath)

If your contact is displayed in the cell you must already have fetched it. So it does not make any sense to fetch it again. Also you have an unused variable (entity) in your very verbose but unnecessary code.

The best way is to use a segue in storyboard from the cell to the edit controller. You can then set up the object in prepareForSegue .

One nice setup is to subclass the table view cell and give it a property of type Contact . The displaying of the attributes in the cell can be handled by the subclass, helping you to uncluttered the table view controller. You can then easily retrieve the object in prepareForSegue from the sender parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM