这是我的Realm对象:
class Transaction: Object {
@objc dynamic var transactionAmount = 0.0
@objc dynamic var transactionDate = Date()
@objc dynamic var transactionName = ""
}
如何根据transactionAmount属性返回1或2个点?
这是我脑海中的代码:
func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
let allTransactions = realm.objects(Transaction.self)
for transaction in allTransactions {
if transaction.transactionAmount > 0 && transaction.transactionAmount < 0 {
return 2
} else if transaction.transactionAmount > 0 || transaction.transactionAmount < 0 {
return 1
}
}
return 0
}
或者如果我使用谓词:
func negativeTransactionPredicate(date: Date) -> NSPredicate {
let calendar = Calendar(identifier: Calendar.Identifier.gregorian)
var components = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date)
components.hour = 00
components.minute = 00
components.second = 00
let startDate = calendar.date(from: components)
components.hour = 23
components.minute = 59
components.second = 59
let endDate = calendar.date(from: components)
return NSPredicate(format: "transactionDate >= %@ && transactionDate =< %@ && transactionAmount < 0", argumentArray: [startDate!, endDate!])
}
然后使用谓词过滤结果:
let incomeTransaction = realm.objects(Transaction.self).filter(positiveTransactionPredicate(date: date))
let expenseTransaction = realm.objects(Transaction.self).filter(negativeTransactionPredicate(date: date))
let incomeAndExpenseTransactions = realm.objects(Transaction.self).filter(positiveAndNegativeTransactionPredicate(date: date))
if incomeAndExpenseTransactions.contains(date) {
return 2
} else if incomeTransaction.contains(date) || expenseTransaction.contains(date) {
return 1
}
从3月份开始我还是编程新手,所以我不完全了解如何调用numberOfEventsFor委托,而且我真的不知道如何将其与Realm相关联。
我非常感谢您的帮助。