任何人都可以告訴我我在這裏做錯了嗎?我有一個我正在設計的小應用程序,它是從我的核心數據中添加,編輯和刪除用戶。但是,當我選擇一行時,我的應用程序崩潰,應該把我帶到下一個viewController。 錯誤是:'致命錯誤:意外地發現零,而解包一個可選值' 它在這個鑄造中準備segue函數失敗:讓聯繫人:Contact = fetchedResultController.objectAtIndexPath(indexPath!)as!聯繫Segue查看控制器崩潰
所以當我點擊一行時,它必須繼續下一個視圖控制器。
這是我的代碼:
//類ContactsTableViewController
import UIKit
import CoreData
class ContactsTableViewController: UITableViewController, NSFetchedResultsControllerDelegate{
// MARK: - Properties
let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var fetchedResultController: NSFetchedResultsController = NSFetchedResultsController()
// MARK: - Actions
@IBAction func toggleMe(sender: AnyObject) {
UberSideBar.toggleWindow()
}
override func viewDidLoad() {
super.viewDidLoad()
fetchedResultController = getFtechedResultController()
fetchedResultController.delegate = self
fetchedResultController.performFetch(nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
let numberOfSection = fetchedResultController.sections?.count
return numberOfSection!
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let numberOfinASection = fetchedResultController.sections?[section].numberOfObjects
return numberOfinASection!
}
// MARK: - Custom functions
func getFtechedResultController() -> NSFetchedResultsController {
fetchedResultController = NSFetchedResultsController(fetchRequest: taskFetchRequest(), managedObjectContext: context!, sectionNameKeyPath: nil, cacheName: nil)
return fetchedResultController
}
func taskFetchRequest() -> NSFetchRequest{
let fetchRequest = NSFetchRequest(entityName: "Contact")
let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
return fetchRequest
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
// Refresh tableview to fetch all data
tableView.reloadData()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ContactCell", forIndexPath: indexPath) as! ContactcellTableViewCell
let contact = fetchedResultController.objectAtIndexPath(indexPath) as! Contact
println(contact.name)
cell.nameLabel?.text = contact.name
return cell
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let managedObject: NSManagedObject = fetchedResultController.objectAtIndexPath(indexPath) as! NSManagedObject
context?.deleteObject(managedObject)
context?.save(nil)
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("editContact", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "editContact" {
let indexPath = tableView.indexPathForSelectedRow()
let contactController: NewCategoryViewController = segue.destinationViewController as! NewCategoryViewController
let contact:Contact = fetchedResultController.objectAtIndexPath(indexPath!) as! Contact
contactController.contact = contact
}
}
} 和
import UIKit
import CoreData
class NewCategoryViewController: UIViewController {
// MARK: - Properties
var contact: Contact? = nil
// initialize the core data context:
let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
// MARK: - Outlets
@IBOutlet weak var imageHolder: UIImageView!
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var phoneField: UITextField!
@IBOutlet weak var categoryField: UITextField!
// MARK: - Actions
@IBAction func savebtn(sender: AnyObject) {
let entity = NSEntityDescription.entityForName("Contact", inManagedObjectContext: context!)
let newContact = Contact(entity: entity!, insertIntoManagedObjectContext: context)
newContact.name = nameField.text
newContact.email = emailField.text
newContact.phone = phoneField.text
//newContact.photo = UIImageJPEGRepresentation(imageHolder.image, 1)
var error: NSError?
context?.save(&error)
if let errorSaving = error {
var alert = UIAlertController(title: "Alert", message: "Couldn't save contact !!!", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} else {
nameField.text = ""
emailField.text = ""
phoneField.text = ""
var alert = UIAlertController(title: "Alert", message: "Contact added", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
if contact != nil {
nameField.text = contact?.name
emailField.text = contact?.email
phoneField.text = contact?.phone
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
任何幫助將是非常讚賞
顯然'sender'不是一個單元對象,它是一個桌面控制器對象 – Paulw11