gpt4 book ai didi

ios - 动态改变cell.update日期函数?

转载 作者:行者123 更新时间:2023-11-29 05:47:45 26 4
gpt4 key购买 nike

我对这一切都是新手,所以请原谅我的无知!我有一个Table View,每次打开场景时我都会调用单元格更新方法(按日期排序)。我试图根据对象上次编辑的时间动态显示不同的日期格式。示例:如果日期是今天,则显示时间;如果日期是昨天,则显示“昨天”等。我的问题是 - 编写此内容的最佳方法是什么?在我的 cell.update 调用中调用以下函数是否会占用大量内存?我怎样才能写得更好?任何帮助将不胜感激!

func update(with fitnessInfo: Fitness) {
if let date = fitnessInfo.dateEdited as Date? {
let today = Calendar.current.isDateInToday(date)
let yesterdayFunc = Calendar.current.isDateInYesterday(date)
let yesterday = date.addingTimeInterval(172800)
let withinSevenDays = date.addingTimeInterval(604800)
let dateFormatter = DateFormatter()

func setdate(_ dateFormatter: DateFormatter) {
let convertedDate = dateFormatter.string(from: date)
fitnessDateLabel.text = convertedDate
}
if today == true {
dateFormatter.dateFormat = "h:mm a"
setdate(dateFormatter)
} else if yesterdayFunc == true {
fitnessDateLabel.text = "Yesterday"
} else {
switch date {
case yesterday...withinSevenDays:
dateFormatter.dateFormat = "EEEE"
setdate(dateFormatter)
default:
dateFormatter.dateFormat = "MMMM dd, yyyy"
setdate(dateFormatter)
}
}
}

这是调用它的 TableView 函数 (cellForRowAt)。

tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: “fitnessCell", for: indexPath) as! FitnessTableViewCell
let fitnessInfo = fetchedRC.object(at: indexPath)
cell.update(with: fitnessInfo)
return cell
}

最佳答案

  1. 如果代码有效,那么它就不会很糟糕。
  2. 在任何性能优化之前编写单元测试和性能测试是一个好主意。因此,您会发现您的更改不会影响结果并提高性能。您还可以使用 Xcode 工具来了解您的代码如何准确地使用 CPU 和内存,方法是单击“产品”->“配置文件”并选择“时间配置文件”和“分配工具”。不要忘记,性能优化通常是内存和 CPU 使用率之间的权衡。
  3. 您应该将逻辑与表示分开。这样您就可以编写单元测试。您还可以静态保留 DateFrommater,而不是在每次绘制单元格时初始化它。以下是如何完成此操作的示例:

    final class DateMapper {
    static let todayDateFormatter = DateFormatter()
    static let lastWeekAgoDateFormatter = DateFormatter()
    static let moreThanWeekAgoDateFormatter = DateFormatter()
    let today: Date
    let currentCalendar = Calendar.current

    init(today: Date) {
    self.today = today
    DateMapper.todayDateFormatter.dateFormat = "h:mm a"
    DateMapper.lastWeekAgoDateFormatter.dateFormat = "EEEE"
    DateMapper.moreThanWeekAgoDateFormatter.dateFormat = "MMMM dd, yyyy"
    }

    func formattedDate(date: Date) -> String {
    guard let totalDays = currentCalendar.dateComponents([.day], from: date, to: today).day else {
    return ""
    }
    switch totalDays {
    case 0:
    return DateMapper.todayDateFormatter.string(from: date)
    case 1:
    return "Yesterday"
    case 2...7:
    return DateMapper.lastWeekAgoDateFormatter.string(from: date)
    default:
    return DateMapper.moreThanWeekAgoDateFormatter.string(from: date)
    }
    }
    }

此类可以在您的更新方法中使用,如下所示:

  func update(with fitnessInfo: Fitness) {
let dateMapper = DateMapper(today: Date())
fitnessDateLabel.text = dateMapper.formattedDate(date: fitnessInfo.dateEdited)
}

或者你可以在ViewController中保留DateMapper并直接在cellForRowAt中使用它。

  1. 此类可以通过单元测试覆盖:

    class ExampleTests: XCTestCase {

    var dateMapper: DateMapper {
    let todayStub = dateStubByComponents(year: 2019, month: 5, day: 4, hour: 12)
    return DateMapper(today: todayStub)
    }

    func testTodayDate() {
    // arrange
    let todayStub = dateStubByComponents(year: 2019, month: 5, day: 4, hour: 11)

    // act
    let result = dateMapper.formattedDate(date: todayStub)

    // assert
    XCTAssertEqual(result, "11:00 AM")
    }

    func testYestadayDate() {
    // arrange
    let yesterdayStub = dateStubByComponents(year: 2019, month: 5, day: 3, hour: 0)

    // act
    let result = dateMapper.formattedDate(date: yesterdayStub)

    // assert
    XCTAssertEqual(result, "Yesterday")
    }

    func testLastWeekDate() {
    // arrange
    let lastWeekStub = dateStubByComponents(year: 2019, month: 5, day: 2, hour: 0)

    // act
    let result = dateMapper.formattedDate(date: lastWeekStub)

    // assert
    XCTAssertEqual(result, "Thursday")
    }

    func testPerformanceExample() {
    let lastWeekStub = dateStubByComponents(year: 2019, month: 5, day: 2, hour: 0)
    self.measure {
    _ = dateMapper.formattedDate(date: lastWeekStub)
    }
    }

    // MARK: Private

    private func dateStubByComponents(year: Int, month: Int, day: Int, hour: Int) -> Date {
    var dateStubComponents = DateComponents()
    dateStubComponents.year = year
    dateStubComponents.month = month
    dateStubComponents.day = day
    dateStubComponents.hour = hour

    guard let dateStub = Calendar.current.date(from: dateStubComponents) else { fatalError("Date must be valid") }
    return dateStub
    }
    }

在控制台,您将看到将日期转换为字符串需要多长时间:

values: [0.000922, 0.000063, 0.000041, 0.000038, 0.000036, 0.000035, 0.000035, 0.000036, 0.000043, 0.000027]

关于ios - 动态改变cell.update日期函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55980135/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com