问题
I am new to programming & trying to apply filters using UISwitches
Currently I have:
- A realm database returning all the objects
- A UI that uses Switches to send filters
Things Attempted:
- Subqueries, however I don't know the proper format to write it for realm
Goal: Apply multiple filters to realm database results
User Interface:
Realm Database:
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;
View Controller:
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()
}
}
来源:https://stackoverflow.com/questions/62247520/how-to-apply-multiple-filters-to-realm-database-results