所以 iv 使用 NSTokenField 来允许数据输入,当用户开始输入时,TokenField 会建议事情。我希望它建议已经存在于核心数据中的东西。
为此,当单元格移动到超级视图时,我会调用此函数(这一切都发生在自定义表格视图单元格内)
var subjectInformation = [NSManagedObject]()
let appDel = NSApplication.sharedApplication().delegate as! AppDelegate
let context = appDel.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "SubjectInformation")
do {
let results = try context.executeFetchRequest(fetchRequest)
subjectInformation = results as! [NSManagedObject]
} catch {
}
这将返回一个 NSManagedObjects 数组,现在我希望为托管对象中的每个对象获取 valueForKey("subjectName") 并将其插入到字符串数组中,以便我可以在此令牌字段中返回它
func tokenField(tokenField: NSTokenField, completionsForSubstring substring: String, indexOfToken tokenIndex: Int, indexOfSelectedItem selectedIndex: UnsafeMutablePointer<Int>) -> [AnyObject]? {
return subjectInformation //this is where is should return an array eg; ["English","Maths","Science"]
我该怎么做?谢谢 :)