gpt4 book ai didi

ios - 是否可以在没有 UITableView 的情况下从 Sqlite(和 CoreData)检索数据 - Swift 3

转载 作者:行者123 更新时间:2023-11-28 17:50:30 26 4
gpt4 key购买 nike

我有一个包含大约 1000 条记录的数据库 (sqlite)。我想不使用 UITableView(索引)而是通过属性的键(即名称)访问这些记录。

我遇到了很大的问题,因为所有教程都使用 index.row

这个逻辑错了吗?我应该使用其他东西吗?

编辑:

到目前为止我已经设法做到了这一点:

  // MARK: - Fetched results controller

var predicateString = "some string"


var fetchedResultsController: NSFetchedResultsController<Anatomy> {
if _fetchedResultsController != nil {
return _fetchedResultsController!
}

let coreDataStack = CoreDataStack()
managedObjectContext = coreDataStack.persistentContainer.viewContext


let fetchRequest: NSFetchRequest<Anatomy> = Event.fetchRequest()
let entity = NSEntityDescription.entity(forEntityName: "Event", in: self.managedObjectContext!)
fetchRequest.entity = entity

myPredicate = NSPredicate(format: "nameEng contains[c] %@", predicateString)
fetchRequest.predicate = myPredicate

fetchRequest.fetchBatchSize = 20

// Edit the sort key as appropriate.
let sortDescriptor = NSSortDescriptor(key: "sortID", ascending: false)

fetchRequest.sortDescriptors = [sortDescriptor]

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: "Master")
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController

do {
try _fetchedResultsController!.performFetch()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}

return _fetchedResultsController!
}

var _fetchedResultsController: NSFetchedResultsController<Anatomy>? = nil

func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
//do sth
}

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
//do sth
}

但我现在遇到了麻烦:

   let object = self.fetchedResultsController......  as! Event //cannot cast this to Event and don t know how to get the values now.... 

print(object.buttonTag)
//this is where indexPath comes and "messes" things around for me....

最佳答案

如果您不使用 tableView,我不会为 FRC 而烦恼 - 一个标准的获取就足够了:

let coreDataStack = CoreDataStack()
managedObjectContext = coreDataStack.persistentContainer.viewContext

let fetchRequest = Event.fetchRequest()

let myPredicate = NSPredicate(format: "nameEng contains[c] %@", predicateString)
fetchRequest.predicate = myPredicate

do {
let results = try context.fetch(fetchRequest) as! [Event]
let myEvent = results[0]
// Do whatever you need with the Event...

} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}

关于ios - 是否可以在没有 UITableView 的情况下从 Sqlite(和 CoreData)检索数据 - Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39789964/

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