答案 0 :(得分:7)
首先,建议将许多关系命名为复数(notes
),以便更容易理解模型。
如果您有一个NSManagedObject
子类并且对文件夹的引用只是通过关系得到注释:
let notes = folder.notes
然而,这会返回Set
。如果你想要一个数组写
let notes = folder.notes.allObjects as! [Note]
您还可以使用谓词分配给Note
实体上的抓取请求:
let name = "Foo"
let predicate = NSPredicate(format:"folder.name == %@", name)
答案 1 :(得分:0)
在你的func中写下这行代码
let container = (UIApplication.shared.delegate as! AppDelegate).persistentContainer
let context: NSManagedObjectContext = container.viewContext
let request:NSFetchRequest<Notes> = Notes.fetchRequest()
request.predicate = NSPredicate(format: "here write relationship name .folderName = %@" , searchbar.text!)
}
do
{
let result = try context.fetch(request)
for notes in result
{
....
}
}