gpt4 book ai didi

ios - 如何从 Swift 中的特定关系实体中获取数据?

转载 作者:行者123 更新时间:2023-11-29 01:43:26 25 4
gpt4 key购买 nike

我在核心数据中有两个关系实体。在第一个实体中,我在属性中添加公司名称。然后我移动到第二个 UIViewController 并在与第一个实体有关系的第二个实体中添加特定公司的其他属性。我还有两个 UITableViewController,第一个 UITableViewController 显示单元格中每个公司的名称。当我选择特定公司时,我移动到第二个 UITableViewController。我希望第二个 UITableViewController 显示我选择的特定公司的数据。我尝试了几种不同的方法,但我无法按照我想要的方式制作。我怎样才能做到?

第二个UITableViewController代码

override func viewDidLoad() {
super.viewDidLoad()
self.definesPresentationContext = true
println(currentCompany)

fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest(), managedObjectContext: managedObjectContext, sectionNameKeyPath: "activityCompany", cacheName: nil)
fetchedResultsController.delegate = self
fetchedResultsController.performFetch(nil)
}

override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest(), managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
self.tableView.reloadData()
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - var and let
var currentCompany: String!
var managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext!

// MARK: - fetchResultsController

var fetchedResultsController: NSFetchedResultsController!

func fetchRequest() -> NSFetchRequest {
let fetchRequest = NSFetchRequest(entityName: "Information")
var sortDescriptor = NSSortDescriptor(key: "company", ascending: true)
fetchRequest.fetchBatchSize = 50
fetchRequest.sortDescriptors = [sortDescriptor]
fetchRequest.predicate = NSPredicate(format: "company contains [c] %@", currentCompany)
return fetchRequest
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return fetchedResultsController.sections?.count ?? 0
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fetchedResultsController.sections?[section].numberOfObjects ?? 0
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
if var dataForCell = fetchedResultsController.objectAtIndexPath(indexPath) as? Information {
cell.textLabel?.text = dataForCell.activityCompany
cell.detailTextLabel?.text = dataForCell.foundedCompany
}

return cell
}

实体

import Foundation
import CoreData

@objc(Company)
class Company: NSManagedObject {

@NSManaged var nameCompany: String
@NSManaged var information: NSSet

}

import Foundation
import CoreData

@objc(Information)
class Information: NSManagedObject {

@NSManaged var activityCompany: String
@NSManaged var foundedCompany: String
@NSManaged var company: Company

}

我通过以下方法将数据保存到实体中。

// MARK: - @IBActions
@IBAction func saveData(sender: UIBarButtonItem) {
var companyEntity = NSEntityDescription.insertNewObjectForEntityForName("Company", inManagedObjectContext: managedObjectContext) as! NSManagedObject
var informationEntity = NSEntityDescription.insertNewObjectForEntityForName("Information", inManagedObjectContext: managedObjectContext) as! NSManagedObject

companyEntity.setValue(currentName, forKey: "nameCompany")

informationEntity.setValue(activityTextField.text, forKey: "activityCompany")
informationEntity.setValue(foundedTextField.text, forKey: "foundedCompany")

companyEntity.setValue(NSSet(object: informationEntity), forKey: "information")
var error: NSError?
managedObjectContext.save(&error)

var story = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
var naviController = story.instantiateViewControllerWithIdentifier("firstNavi") as! UINavigationController
presentViewController(naviController, animated: true, completion: nil)
}

最佳答案

我解决了。

   func fetchRequest() -> NSFetchRequest {
let fetchRequest = NSFetchRequest(entityName: "Information")
var sortDescriptor = NSSortDescriptor(key: "activityCompany", ascending: true)
fetchRequest.fetchBatchSize = 50
fetchRequest.sortDescriptors = [sortDescriptor]
fetchRequest.predicate = NSPredicate(format: "company.nameCompany contains [c] %@", currentCompany)
return fetchRequest
}

关于ios - 如何从 Swift 中的特定关系实体中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32168954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com