简体   繁体   English

iOS 8 Swift:使用segue编辑NSManagedObject:声明类型时出错

[英]iOS 8 Swift: editing NSManagedObject with segue: error when declaring types

So I'm having some trouble wrapping my head around how to edit/update objects in Core Data. 因此,我在解决如何编辑/更新Core Data中的对象时遇到了一些麻烦。

What I'm trying to do is in my DetailViewController have two segues that push to my AddTableViewController . 我想做的是在我的DetailViewController有两个选项可以推送到我的AddTableViewController

DetailViewController : DetailViewController

import UIKit
import Social
import CoreData



class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet var backpackerSpotImageView:UIImageView!
    @IBOutlet var tableView:UITableView!


    var backpackerSpot:BackpackerSpot?
    var managedContext: NSManagedObjectContext!

    var backpackerSpots:[BackpackerSpot] = []

    var fetchResultController:NSFetchedResultsController!


    override func viewDidLoad() {
        super.viewDidLoad()

        // customizing background of tableview
        self.tableView.backgroundColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 0.2)

        // remove extra separators
        self.tableView.tableFooterView = UIView(frame: CGRectZero)

        // change the color of the separator
        self.tableView.separatorColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 0.8)

        // self-sizing cells
        tableView.estimatedRowHeight = 36.0
        tableView.rowHeight = UITableViewAutomaticDimension

        // Do any additional setup after loading the view.
        if let spotImage = backpackerSpot?.spotImage
        {
        self.backpackerSpotImageView.image = UIImage(data:spotImage)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! DetailTableViewCell

        // make cell transparent so background color can be seen
        cell.backgroundColor = UIColor.clearColor()

        cell.mapButton.hidden = true

        switch indexPath.row {
        case 0:
            cell.fieldLabel.text = "Name"
            cell.valueLabel.text = backpackerSpot?.spotName
        case 1:
            cell.fieldLabel.text = "Location"
            cell.valueLabel.text = backpackerSpot?.spotLocation
            cell.mapButton.hidden = false
        case 2:
            cell.fieldLabel.text = "Notes"
            cell.valueLabel.text = backpackerSpot?.spotNote
        default:
            cell.fieldLabel.text = ""
            cell.valueLabel.text = ""

        }

        return cell
    }

//    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
//        return true
//    }
//    
//    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
//        if(editingStyle == .Delete) {
//            // Find the BackPacker Spot object the user is trying to delete
//            let backpackerSpotToDelete = backpackerSpots[indexPath.row]
//            
//            // Delete it from the managedObjectContext
//            managedContext?.deleteObject(backpackerSpotToDelete)
//            
//           reloadInputViews()
//            
//
//        }
//    }

    @IBAction func shareSheet(sender:UIBarButtonItem) {
        let firstActivityItem = backpackerSpot!.spotName
        let secondActivityItem = backpackerSpot!.spotLocation

        let activityViewController : UIActivityViewController = UIActivityViewController(
            activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil)

        activityViewController.excludedActivityTypes = [
            UIActivityTypePostToVimeo,
            UIActivityTypePostToTencentWeibo,
            UIActivityTypePostToFlickr,
            UIActivityTypePostToWeibo,
            UIActivityTypeSaveToCameraRoll,
            UIActivityTypePrint,
            UIActivityTypeAssignToContact,
            UIActivityTypeAddToReadingList
        ]

        self.presentViewController(activityViewController, animated: true, completion: nil)

    }

    // MARK: - Navigation

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "showMap" {
            let destinationController = segue.destinationViewController as! MapViewController
            destinationController.backpackerSpot = backpackerSpot
        } else if segue.identifier == "editSpot"{
            var selectedItem: NSManagedObject = backpackerSpot!
            let destinationController = segue.destinationViewController as! AddTableViewController

            destinationController.existingName = selectedItem.valueForKey("spotName") as! String
            destinationController.existingLocation = selectedItem.valueForKey("spotLocation") as! String
            destinationController.existingNotes = selectedItem.valueForKey("spotNote") as! String
            destinationController.existingImage = selectedItem.valueForKey("spotImage") as! NSData
            destinationController.existingSpot = selectedItem
            destinationController.backpackerSpot = backpackerSpot

            }
        }

    }

AddTableViewController:

import UIKit
import CoreData

class AddTableViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var nameTextField:UITextField!
    @IBOutlet weak var locationTextField:UITextField!
    @IBOutlet weak var imageView:UIImageView!
    @IBOutlet weak var notesView:UITextView!

    var existingName:String = ""
    var existingLocation:String = ""
    var existingNotes:String = ""
    var existingImage:NSData!
    var existingSpot: NSManagedObject!


    var coreDataStack = (UIApplication.sharedApplication().delegate as! AppDelegate).coreDataStack

    var backpackerSpot:BackpackerSpot!
    var managedContext: NSManagedObjectContext!

    override func viewDidLoad() {
        super.viewDidLoad()
        managedContext = coreDataStack.context

        if (existingSpot != nil) {
            nameTextField.text = existingName
            locationTextField.text = existingLocation
            imageView.image = existingImage
            notesView.text = existingNotes
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.

    }

    // TODO Give user the choice of the Photo Library or the Camera
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row == 0 {
            if UIImagePickerController.isSourceTypeAvailable(.Camera) {
                let imagePicker = UIImagePickerController()
                imagePicker.allowsEditing = false
                imagePicker.delegate = self
                imagePicker.sourceType = .Camera

                self.presentViewController(imagePicker, animated: true, completion: nil)
            }
        }

        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }

    // FIXME image is being displayed in landscape if it is taken in portrait mode by default
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
        imageView.image = image
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        imageView.clipsToBounds = true

        dismissViewControllerAnimated(true, completion: nil)
    }

    @IBAction func save() {

        //validation
        var errorField = ""

        // TODO have placeholder text in the NOTES field match up with the placholder text in the NAME and LOCATION fields.
        if nameTextField.text == "" {
            errorField = "name"
        } else if locationTextField.text == "" {
            errorField = "location"
        } else if notesView.text == "" {
            errorField = "notes"
        }

        if errorField != "" {

            let alertController = UIAlertController(title: "Error", message: "You must fill in \(errorField).", preferredStyle: .Alert)
            let doneAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
            alertController.addAction(doneAction)

            self.presentViewController(alertController, animated: true, completion: nil)

            return
        }

        // If all fields are correctly filled in, extract the field value
        // Create Restaurant Object and save to data store
//        if let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).coreDataStack.context {

            let entityBackpackerSpot = NSEntityDescription.entityForName("BackpackerSpot", inManagedObjectContext: coreDataStack.context)

            backpackerSpot = BackpackerSpot( entity: entityBackpackerSpot!, insertIntoManagedObjectContext: managedContext )

            backpackerSpot?.spotName = nameTextField.text
            backpackerSpot?.spotLocation = locationTextField.text
            backpackerSpot?.spotImage = UIImagePNGRepresentation(imageView.image)
            backpackerSpot?.spotNote = notesView.text

            var error: NSError?
            if !managedContext.save(&error) {
                println("insert error: \(error!.localizedDescription)")
                return
            }

        // Execute the unwind segue and go back to the home screen
        performSegueWithIdentifier("unwindToHomeScreen", sender: self)
    }

}

I know that I do not yet have the necessary functions to edit the data in my AddTableViewController , but the first issue I'm having is even passing the data. 我知道我还没有必要的功能来编辑AddTableViewController的数据,但是我AddTableViewController的第一个问题甚至是传递数据。 I'm currently getting the following error: 我目前遇到以下错误:

AddTableViewController.swift:38:31: Cannot assign a value of type 'NSData!' to a value of type 'UIImage?'

I tried casting the same variable to UIImage but that also gave me an error (which I expected). 我尝试将相同的变量强制转换为UIImage但这也给了我一个错误(这是我期望的)。

Am I on the right track, or should I edit/update the object in a completely different way? 我是在正确的轨道上吗,还是应该以完全不同的方式编辑/更新对象? I've been working with CoreData for a few weeks now and I'm starting to get the hang of it, but as I said before, I can't exactly wrap my head around passing the data. 我已经与CoreData合作了几周了,现在我开始掌握它了,但是正如我之前说的,我无法全神贯注地传递数据。

Any help is appreciated. 任何帮助表示赞赏。 Thank you. 谢谢。

You need to create UIImage with your NSData first, try: 您需要先使用NSData创建UIImage,然后尝试:

if (existingSpot != nil) {
        nameTextField.text = existingName
        locationTextField.text = existingLocation
        imageView.image = UIImage(data: existingImage)
        notesView.text = existingNotes
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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