假设我有一个Event
如下课程:
class Event {
private var attendees: [Person] = []
// Case 1
//*******
// Should I use a func…
func countOfAttendees() -> Int {
return attendees.count
}
// …or a var
var countOfAttendees: Int {
return attendees.count
}
// Case 2
//*******
// Should I use a func…
func countOfPaidAttendees() -> Int {
return attendees.filter({$0.hasPaid}).count
}
// …or a var
var countOfPaidAttendees: Int {
return attendees.filter({$0.hasPaid}).count
}
}
在上述两种情况下使用函数或计算属性是最佳实践吗?