我是Swift的新手,我遇到了提取请求的问题。 请求为零,因此它不会向我显示我想要的数据。但是我之前在UebungenMuskelgruppenTableViewController中做了同样的工作。只有PlanMuskelgruppenViewController中的请求为零,但我使用了相同的实体。布尔uebungForPlan也是零,所以segue不会将变量传递给UebungAnzeigenViewController,我不知道为什么。这是我的代码:
class UebungenMuskelgruppenTableViewController: UITableViewController {
var mgdContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var uebungen = [Uebung]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let uvc = segue.destinationViewController as? UebungenAnzeigenViewController {
if let identifier = segue.identifier{
switch identifier{
case "Brust":
let request = NSFetchRequest(entityName: "Uebung")
request.predicate = NSPredicate(format: "muskel == %@", "Brust")
do{
try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
}
catch{
print("Abfrage fehlgeschlagen!")
}
uvc.request2 = request
case "Rücken":
let request = NSFetchRequest(entityName: "Uebung")
request.predicate = NSPredicate(format: "muskel == %@", "Rücken")
do{
try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
}
catch{
print("Abfrage fehlgeschlagen!")
}
uvc.request2 = request
case "Beine":
let request = NSFetchRequest(entityName: "Uebung")
request.predicate = NSPredicate(format: "muskel == %@", "Beine")
do{
try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
}
catch{
print("Abfrage fehlgeschlagen!")
}
uvc.request2 = request
case "Schultern":
let request = NSFetchRequest(entityName: "Uebung")
request.predicate = NSPredicate(format: "muskel == %@", "Schultern")
do{
try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
}
catch{
print("Abfrage fehlgeschlagen!")
}
uvc.request2 = request
case "Bizeps":
let request = NSFetchRequest(entityName: "Uebung")
request.predicate = NSPredicate(format: "muskel == %@", "Bizeps")
do{
try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
}
catch{
print("Abfrage fehlgeschlagen!")
}
uvc.request2 = request
case "Trizeps":
let request = NSFetchRequest(entityName: "Uebung")
request.predicate = NSPredicate(format: "muskel == %@", "Trizeps")
do{
try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
}
catch{
print("Abfrage fehlgeschlagen!")
}
uvc.request2 = request
case "Nacken":
let request = NSFetchRequest(entityName: "Uebung")
request.predicate = NSPredicate(format: "muskel == %@", "Nacken")
do{
try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
}
catch{
print("Abfrage fehlgeschlagen!")
}
uvc.request2 = request
case "Unterarme":
let request = NSFetchRequest(entityName: "Uebung")
request.predicate = NSPredicate(format: "muskel == %@", "Unterarme")
do{
try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
}
catch{
print("Abfrage fehlgeschlagen!")
}
uvc.request2 = request
default:
break
}
}
}
}
这是来自Viewcontroller的代码,请求为nil:
class PlanMuskelgruppenViewController: UIViewController {
var mgdContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var uebungen = [Uebung]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let uvc = segue.destinationViewController as? UebungenAnzeigenViewController {
if let identifier = segue.identifier{
switch identifier{
case "BrustPlan":
let request = NSFetchRequest(entityName: "Uebung")
request.predicate = NSPredicate(format: "muskel == %@", "Brust")
do{
try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
}
catch{
print("Abfrage fehlgeschlagen!")
}
uvc.request2 = request
uvc.uebungForPlan = true
case "RückenPlan":
let request = NSFetchRequest(entityName: "Uebung")
request.predicate = NSPredicate(format: "muskel == %@", "Rücken")
do{
try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
}
catch{
print("Abfrage fehlgeschlagen!")
}
uvc.request2 = request
uvc.uebungForPlan = true
case "BeinePlan":
let request = NSFetchRequest(entityName: "Uebung")
request.predicate = NSPredicate(format: "muskel == %@", "Beine")
do{
try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
}
catch{
print("Abfrage fehlgeschlagen!")
}
uvc.request2 = request
uvc.uebungForPlan = true
case "SchulternPlan":
let request = NSFetchRequest(entityName: "Uebung")
request.predicate = NSPredicate(format: "muskel == %@", "Schultern")
do{
try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
}
catch{
print("Abfrage fehlgeschlagen!")
}
uvc.request2 = request
uvc.uebungForPlan = true
case "BizepsPlan":
let request = NSFetchRequest(entityName: "Uebung")
request.predicate = NSPredicate(format: "muskel == %@", "Bizeps")
do{
try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
}
catch{
print("Abfrage fehlgeschlagen!")
}
uvc.request2 = request
uvc.uebungForPlan = true
case "TrizepsPlan":
let request = NSFetchRequest(entityName: "Uebung")
request.predicate = NSPredicate(format: "muskel == %@", "Trizeps")
do{
try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
}
catch{
print("Abfrage fehlgeschlagen!")
}
uvc.request2 = request
uvc.uebungForPlan = true
case "NackenPlan":
let request = NSFetchRequest(entityName: "Uebung")
request.predicate = NSPredicate(format: "muskel == %@", "Nacken")
do{
try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
}
catch{
print("Abfrage fehlgeschlagen!")
}
uvc.request2 = request
uvc.uebungForPlan = true
case "UnterarmePlan":
let request = NSFetchRequest(entityName: "Uebung")
request.predicate = NSPredicate(format: "muskel == %@", "Unterarme")
do{
try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
}
catch{
print("Abfrage fehlgeschlagen!")
}
uvc.request2 = request
uvc.uebungForPlan = true
default:
break
}
}
}
}
}
以下是执行请求的代码:
class UebungenAnzeigenViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var mgdContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
@IBOutlet weak var hinzufuegenButtonOutlet: UIBarButtonItem!
@IBOutlet weak var tableView: UITableView!
var uebungen = [Uebung](){
didSet{
self.tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
loadUebungen()
setupTableView()
print("boolean: \(uebungForPlan)")
}
var request2 = NSFetchRequest?()
var uebungForPlan: Bool?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("UebungenCell", forIndexPath: indexPath)
let uebung = uebungen[indexPath.row]
cell.textLabel!.text = uebung.name!
cell.textLabel?.textAlignment = .Center
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return uebungen.count
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if uebungForPlan == true{
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let uebungen = self.uebungen[indexPath.row]
let cell = tableView.cellForRowAtIndexPath(indexPath)
if uebungen.ausgewaehlt == true{
uebungen.ausgewaehlt = false
}
else{
uebungen.ausgewaehlt = true
}
if uebungen.ausgewaehlt == true{
cell?.accessoryType = UITableViewCellAccessoryType.Checkmark
}
else{
cell?.accessoryType = UITableViewCellAccessoryType.None
}
}
}
func loadUebungen(){
if request2 != nil{
do{
try uebungen = mgdContext.executeFetchRequest(request2!) as! [Uebung]
}
catch{
print(error)
}
}}
func setupTableView(){
if uebungForPlan == true{
hinzufuegenButtonOutlet.title = "Zu Plan hinzufügen"
}
}
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
if identifier == "neueUebung"{
if uebungForPlan == true{
return false
}
else{
return true
}
}
return false
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if uebungForPlan == nil{
if segue.identifier == "neueUebung" {
let ctrl = segue.destinationViewController as! UebungHinzufuegenViewController
ctrl.saveDelegate = {
(newUebung, newMuskelgruppe) in
let neueUebung = NSEntityDescription.insertNewObjectForEntityForName("Uebung", inManagedObjectContext: self.mgdContext) as! Uebung
neueUebung.name = newUebung
neueUebung.muskel = newMuskelgruppe
do{
try self.mgdContext.save()
}
catch{
print("speichern fehlgeschlagen")
}
self.loadUebungen()
self.navigationController?.popViewControllerAnimated(true)
}
}
}
}
}
抱歉我的英文不好,感谢您的帮助,我尝试了许多清除项目,重置模拟器的事情,几天前我完成了应用程序的重建,没有任何作用。