gpt4 book ai didi

ios - 核心数据值快速访问错误

转载 作者:搜寻专家 更新时间:2023-11-01 05:43:04 25 4
gpt4 key购买 nike

我正在使用 Core Data。执行提取并重新加载后,UITableView 应用崩溃。

 override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell?
{
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

tasks = self.fetchedResultsController.objectAtIndexPath(indexPath) as Tasks
cell.textLabel.text = tasks?.desc

return cell
}

我知道错误在这部分->

    tasks = self.fetchedResultsController.objectAtIndexPath(indexPath) as Tasks

在阅读了一些 StackOverflow 问题后,我将这段代码添加到我现有的 cellForRowAtIndexPath UITableView 委托(delegate)方法中。

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell?
{
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

var array : NSArray = self.fetchedResultsController.fetchedObjects as NSArray
cell.textLabel.text = array.valueForKeyPath("desc") as NSString

return cell
}

此时应用再次崩溃。

   var array : NSArray = self.fetchedResultsController.fetchedObjects as NSArray

我用这段代码打印了这个

println(self.fetchedResultsController.fetchedObjects)

[<NSManagedObject: 0x16debcc0> (entity: Tasks; id: 0x16dd02f0 <x-coredata://A5AEB550-EB6D-4A47-9F6F-2F3AA49CA6BF/Tasks/p2> ; data: {
desc = gghjg;
}), <NSManagedObject: 0x16ebb1d0> (entity: Tasks; id: 0x16e8cda0 <x-coredata://A5AEB550-EB6D-4A47-9F6F-2F3AA49CA6BF/Tasks/p1> ; data: {
desc = gxshli;
})]

我不知道这有什么问题。我按照本文中的步骤操作 Core Data Example .

如果有人知道这有什么问题,请告诉我。谢谢。

这是崩溃报告的图片。 enter image description here

这是我的 ViewController 源文件类的完整代码。

import UIKit
import CoreData

class MaterTableViewController: UITableViewController, UIAlertViewDelegate, NSFetchedResultsControllerDelegate {

var fetchedResultsController : NSFetchedResultsController = NSFetchedResultsController()
let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext
var tasks : Tasks? = nil


override func viewDidLoad()
{
super.viewDidLoad()

self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: Selector("insertNewObject"))

// Update the ViewDidLoad method of TableViewController to populate fetchedResultController variable and set the delegate to self and call the function to retrieve the results.

fetchedResultsController = getFetchedResultController()
fetchedResultsController.delegate = self
fetchedResultsController.performFetch(nil)

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

// MARK: - Core Data Methods

func insertNewObject()
{
if tasks != nil
{
self.editTasks()
}
else
{
var alertView : UIAlertView = UIAlertView()
alertView.title = "Add Note"
alertView.alertViewStyle = UIAlertViewStyle.PlainTextInput
alertView.addButtonWithTitle("Cancel")
alertView.addButtonWithTitle("Save")
alertView.delegate = self
alertView.show()

}

}

func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int)
{
switch buttonIndex
{
case 0 : alertView .dismissWithClickedButtonIndex(buttonIndex, animated: true)
case 1 :
tempData = alertView.textFieldAtIndex(0).text
self.saveThisValue(tempData)
default : println("Nothin")
}
}

var tempData : NSString = NSString()

func editTasks()
{
tasks?.desc = tempData
managedObjectContext?.save(nil)
}

func saveThisValue(input : NSString)
{

let entityDescription = NSEntityDescription.entityForName("Tasks", inManagedObjectContext: managedObjectContext)
let task = Tasks(entity: entityDescription, insertIntoManagedObjectContext: managedObjectContext)
task.desc = input
managedObjectContext?.save(nil)

}

func getFetchedResultController() -> NSFetchedResultsController
{
fetchedResultsController = NSFetchedResultsController(fetchRequest: taskFetchRequest(), managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
return fetchedResultsController
}

func taskFetchRequest() -> NSFetchRequest
{
let fetchRequest = NSFetchRequest(entityName: "Tasks")
let sortDecriptor = NSSortDescriptor(key: "desc", ascending: true)
fetchRequest.sortDescriptors = [sortDecriptor]
return fetchRequest
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {

return fetchedResultsController.sections.count
}

override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {


return fetchedResultsController.sections[section].numberOfObjects
}


override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell?
{
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

tasks = self.fetchedResultsController.objectAtIndexPath(indexPath) as Tasks
cell.textLabel.text = tasks?.desc

return cell
}


func controllerDidChangeContent(controller: NSFetchedResultsController!)
{
fetchedResultsController.performFetch(nil)
tableView.reloadData()
}
}

最佳答案

尝试

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell?
{
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

let task = self.fetchedResultsController.objectAtIndexPath(indexPath.row) as Tasks
cell.textLabel.text = task.desc


return cell
}

关于ios - 核心数据值快速访问错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25636303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com