类型的价值"实体"没有会员'文字'

时间:2016-07-28 16:33:02

标签: ios swift xcode

我正在制作基本笔记应用程序。我似乎无法在代码中更改属性的文本。有什么建议?

import UIKit
import CoreData

class NoteInputTableViewController: UITableViewController, UINavigationControllerDelegate {

var listItem: List?

let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

@IBOutlet weak var inputNoteLabel: UITextField!

func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {

    if let item = listItem {
        listItem.text = inputNoteLabel.text
    } else {
        let newItem = NSEntityDescription.insertNewObjectForEntityForName("List", inManagedObjectContext: managedObjectContext) as! List
        newItem.text = inputNoteLabel.text
    }

    if let controller = viewController as? NoteHomeTableViewController {
        controller.items = [items!] 
    }
}





override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.delegate = self

} // viewDidLoad end

}

在我的navigationController中,我试图更改listItem的文本,但似乎无法这样做。

下面是我的List + CoreDataProperties.swift文件:

import Foundation
import CoreData

extension List {

    @NSManaged var note: String?
    @NSManaged var completionDate: NSDate?

}

以下是我的NoteHomeTableViewController.swift文件:

import UIKit
import CoreData

class NoteHomeTableViewController: UITableViewController, MyCellDelegate { // will need a delegate

var items: [List] = []

let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

@IBAction func composeButtonPressed(sender: UIBarButtonItem) {
    performSegueWithIdentifier("CellDetailSegue", sender: nil)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let destination = segue.destinationViewController as! NoteInputTableViewController
    if let ip = sender as? NSIndexPath {
        destination.listItem = items[ip.row]

    }
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("MyCell") as! MyCell

    cell.indexPath = indexPath
    cell.item = items[indexPath.row]
    cell.delegate = self


    return cell
}

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

    performSegueWithIdentifier("CellDetailSegue", sender: indexPath)
}

func myCell(myCell: MyCell, didPressBackItem: List,
            atIndexPath indexPath: NSIndexPath) {

}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    let item = items[indexPath.row]
    managedObjectContext.deleteObject(item)
    do {
        try managedObjectContext.save()
    } catch {
        print("error saving")
    }
    fetchItems()
    tableView.reloadData()

}

override func viewDidLoad() {
    super.viewDidLoad()

} // viewDidLoad end

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)

    fetchItems()
    tableView.reloadData()
} // viewDidAppear end

func fetchItems() {
    let request = NSFetchRequest(entityName: "List")
    items = []
    do {
        let response = try managedObjectContext.executeFetchRequest(request) as! [List]
        for item in response {
            items.append(item)
            }
    } catch {
        print("Error fetching data!")
    }
} // fetch end

} // class end

2 个答案:

答案 0 :(得分:0)

我不知道你在哪里初始化listItem

我建议

} else {
  let newItem = NSEntityDescription.insertNewObjectForEntityForName("List", inManagedObjectContext: managedObjectContext) as! List
  newItem.text = inputNoteLabel.text
}

应该成为

} else {
  let newItem = NSEntityDescription.insertNewObjectForEntityForName("List", inManagedObjectContext: managedObjectContext) as! List
  listItem = newItem
  newItem.text = inputNoteLabel.text
}

答案 1 :(得分:0)

在可选绑定后使用展开的item,并将新初始化的NSManagedObject分配给listItem

...    
if let item = listItem {
    item.text = inputNoteLabel.text
} else {
    listItem = NSEntityDescription.insertNewObjectForEntityForName("List", inManagedObjectContext: managedObjectContext) as! List
    listItem!.text = inputNoteLabel.text
...

或更容易

...    
if listItem == nil {
    listItem = NSEntityDescription.insertNewObjectForEntityForName("List", inManagedObjectContext: managedObjectContext) as! List
}
listItem!.text = inputNoteLabel.text
...