我在tableView1中有一个Person类,然后在TableView2中,用户添加品牌以与该人关联。我的获取请求正在尝试返回他们选择的人,并使用该人的关联品牌填充tableView的行,但我在这里卡住了:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(Storyboard.CellReuseIdentifier, forIndexPath: indexPath) as! UITableViewCell
let brandSetAllObjects = brandSet?.allObjects
let brand = brandSetAllObjects as? Brand[indexPath.row]
cell.textLabel!.text = brand.valueForKey("name") as? String
return cell
}
获取:
//Helper Function to Fetch Core Data
func fetchCoreData() {
//1
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!
//2
let fetchRequest = NSFetchRequest(entityName:"Person")
//3
var error: NSError?
let fetchedResults =
managedContext.executeFetchRequest(fetchRequest,
error: &error) as? [NSManagedObject]
if let results = fetchedResults as? [Person] {
for selectedPerson in results {
brandSet = selectedPerson.brands
println(brandSet)
}
} else {
println("Could not fetch \(error), \(error!.userInfo)")
}
类:
import Foundation
import CoreData
class Brand: NSManagedObject {
@NSManaged var name: String
@NSManaged var people: NSSet
}
import Foundation
import CoreData
class Person: NSManagedObject {
@NSManaged var name: String
@NSManaged var brands: NSSet
func addBrandsObject(value: Brand) {
self.mutableSetValueForKey("brands").addObject(value)
}
}
let brand = brandSetAllObjects as? Brand[indexPath.row]
? - Unheilig