我是编程新手并尝试使用 UISwitches 应用过滤器
目前我有:
- 返回所有对象的领域数据库
- 使用 Switch 发送过滤器的 UI
尝试的事情:
- 子查询,但是我不知道为领域编写它的正确格式
目标:对领域数据库结果应用多个过滤器
用户界面:
领域数据库:
class Question: Object {
@objc dynamic var neverAttempted: Bool = true
@objc dynamic var correct: Int = 0
@objc dynamic var flagged: Bool = false
@objc dynamic var subject: String = ""
}
correct = 1;
neverAttempted = true;
flagged = false;
subject = Law;
视图控制器:
import UIKit
import RealmSwift
class OptionsViewController: UIViewController {
@IBOutlet weak var totalQuestionsLabel: UILabel!
@IBOutlet weak var totalQuestionSlider: UISlider!
var neverAttempted = ""
var correct = ""
var flagged = false
var subject = ""
let realmOne = try! Realm()
var questionBank: Results<Question>! {
get {
return realmOne.objects(Question.self)
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
func updateQuestionBank() {
let updatedQuestionBank = realmOne.objects(Question.self)
.filter(//Apply filter//)
totalQuestionsLabel.text = String(updatedQuestionBank.count)
print (updatedQuestionBank)
}
@IBAction func subjectSwitch(_ sender: UISwitch) {
if sender.isOn == true {
subject =
updateQuestionBank()
}
}
@IBAction func flaggedSwitch(_ sender: UISwitch) {
if sender.isOn == true {
flagged =
updateQuestionBank()
}
}
@IBAction func neverAttemptedSwitch(_ sender: UISwitch) {
if sender.isOn == true {
neverAttempted =
}
}
@IBAction func correctOnlyOnceSwitch(_ sender: UISwitch) {
if sender.isOn == true {
correct =
updateQuestionBank()
}
}