Coredata fetchrequest 谓词没有显示正确的东西
Posted
技术标签:
【中文标题】Coredata fetchrequest 谓词没有显示正确的东西【英文标题】:Coredata fetchrequest predicate doesn't show the right things 【发布时间】:2016-04-22 12:25:39 【问题描述】:我是 swift 新手,我尝试使用 CoreData 制作应用程序,但谓词没有显示正确的结果。似乎程序忽略了谓词,因为没有谓词它显示相同的东西。 这是我的代码:
import UIKit
class UebungenAnzeigenTableViewController: 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!")
case "Ruecken":
break
case "Beine":
break
case "Schulter":
break
case "Bizeps":
break
case "Trizeps":
break
case "Nacken":
break
case "Unterarm":
break
default:
break
这里是应该展示练习的地方:
import UIKit
class UebungenAnzeigenViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
var mgdContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
@IBOutlet weak var tableView: UITableView!
var uebungen = [Uebung]()
didSet
self.tableView.reloadData()
override func viewDidLoad()
super.viewDidLoad()
loadUebungen()
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCellWithIdentifier("UebungCell", 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 loadUebungen()
let request = NSFetchRequest(entityName: "Uebung")
do
try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
catch
print(error)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
if segue.identifier == "neueUebung"
let ctrl = segue.destinationViewController as! UebungSaveViewController
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)
.. 我在这里保存了练习:
import UIKit
class UebungSaveViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
var muskelgruppen = ["Brust", "Rücken", "Beine", "Schultern", "Bizeps", "Trizeps", "Nacken", "Unterarme"]
var saveDelegate: ((String, String) ->())?
var selectedMuskelgruppe = "Brust"
@IBOutlet weak var pickerView: UIPickerView!
@IBOutlet weak var nameTextField: UITextField!
@IBAction func saveUebungAction(sender: UIButton)
let neueUebung = nameTextField.text
if saveDelegate != nil && neueUebung != nil
saveDelegate!(neueUebung!, selectedMuskelgruppe)
override func viewDidLoad()
super.viewDidLoad()
self.pickerView.dataSource = self
self.pickerView.delegate = self
self.hideKeyboardWhenTappedAround()
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
return muskelgruppen.count
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
return muskelgruppen[row]
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
return 1
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
switch row
case 0:
selectedMuskelgruppe = "Brust"
case 1:
selectedMuskelgruppe = "Rücken"
case 2:
selectedMuskelgruppe = "Beine"
case 3:
selectedMuskelgruppe = "Schultern"
case 4:
selectedMuskelgruppe = "Bizeps"
case 5:
selectedMuskelgruppe = "Trizeps"
case 6:
selectedMuskelgruppe = "Nacken"
case 7:
selectedMuskelgruppe = "Unterarm"
default:
break
有人知道为什么这不起作用吗?我是 swift 新手,我尝试了很多东西,但我不明白..
【问题讨论】:
【参考方案1】:那是因为您似乎没有谓词:
func loadUebungen()
let request = NSFetchRequest(entityName: "Uebung")
do
try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
catch
print(error)
您确实有一个谓词的地方与实际填充表格的代码断开了连接,因此您获得了一个过滤列表,并且在 segue 方法(在前一个视图控制器中)中什么都不做,但随后没有应用谓词你真正需要它的地方(loadUebungen
)。
【讨论】:
以上是关于Coredata fetchrequest 谓词没有显示正确的东西的主要内容,如果未能解决你的问题,请参考以下文章
将 UITextField 中的文本用于 fetchRequest 谓词
在 UUID 类型属性上使用 @FetchRequest 谓词过滤进行不可靠更新?
如果没有数据,带有 @FetchRequest (SwiftUI) 和 NSPredicate 的 CoreData 会崩溃
更改 NSFetchedResultsController 的 fetchRequest.predicate 不会触发委托