So guys, I'm back with yet another question. I have two tables in my Core Data Stack. One of them is called BookCategory with attribute name. The other is called Book with attribute name. BookCategory has a relationship to Book called books. Book has a relationship to BookCategory called category. I'm trying to start from BookCategory and show all the books which name contains a specific character. My books array keeps returning nil. The names of the book do contain the character though. What am I doing wrong? Thanks again!
Code:
func booksContainingTheLetter(letter: String) {
let fetchRequest = NSFetchRequest(entityName: "BookCategory")
// let text = NSExpression(forKeyPath: "books.name")
// let i = NSExpression(forConstantValue: "i")
// let predicate = NSComparisonPredicate(leftExpression: text, rightExpression: i, modifier: NSComparisonPredicateModifier.AnyPredicateModifier, type: NSPredicateOperatorType.ContainsPredicateOperatorType, options: NSComparisonPredicateOptions.CaseInsensitivePredicateOption)
let predicate = NSPredicate(format: "books.name contains %@", letter)
fetchRequest.predicate = predicate
var books: [Book]?
do {
books = try self.managedObjectContext.executeFetchRequest(fetchRequest) as? [Book]
} catch {
print("No book has the letter \(letter) in its name")
}
for book in books as [Book]! {
print("Book is titled \(book.name!)")
}
}
Last edited: