【发布时间】:2015-12-13 16:12:24
【问题描述】:
我的应用程序有许多单词列表,每个单词列表都包含许多单词。在一个视图控制器中,一个 tableView 列出了 wordList,然后一个子视图控制器有一个 tableView,里面有单词。 segue 传递 wordList 实体。但我无法弄清楚如何对传递的 wordList 执行获取请求,然后获取所有单词。错误信息如下所示。我需要执行获取请求而不是查看属性,以便进行排序。
在此先感谢您对此提供的任何帮助....
WordList实体有一个属性listName,关系:words,destination:Word,Inverse:wordList
Word实体有一个属性wordName、wordIndex和关系:wordList,destination:WordList,Inverse:words
我的 WordsViewController 看起来像:
class WordsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {
var coreDataStack: CoreDataStack = (UIApplication.sharedApplication().delegate as! AppDelegate).coreDataStack
var wordList: WordList?
var words = [Word]()
var word: Word?
override func viewDidLoad() {
super.viewDidLoad()
// set the title of the scene
title = wordList?.listName
// fetch all the words that are part of the wordList passed to this VC.
let fetchRequest = NSFetchRequest(entityName: "Word")
let wordListPredicate = NSPredicate(format: "word.wordList == '\(wordList)'") // GIVES AN ERROR SAYING UNABLE TO PARSE THE FORMAT STRING "word.wordList == 'Optional(<WordList:0x...
let sortDescriptor = NSSortDescriptor(key: "wordIndex", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
fetchRequest.predicate = wordListPredicate
do {
if let results = try coreDataStack.managedObjectContext.executeFetchRequest(fetchRequest) as? [Word] {
words = results
}
} catch {
fatalError("There was an error fetching system person")
}
}
【问题讨论】:
标签: swift core-data nspredicate nsfetchrequest