SwiftData unable to save Timer property of a class?

enter image description here

On Xcode 15.0.1, I’m trying to use SwiftData to create a StopwatchItem in my stopwatch app, which has a timer property. I get the errors:

  1. Instance method ‘setValue(forKey:to:)’ requires that ‘Timer’ conform to ‘PersistentModel’

  2. Instance method ‘getValue(forKey:)’ requires that ‘Timer’ conform to ‘PersistentModel’

I know this means something along the lines of “Cannot save Timer to SwiftData”, but I’m not sure how I can resolve this problem I’m facing.

My stopwatch app would have the ability to create and control multiple stopwatches, which includes global control (Start all, stop all, reset all etc.) and the ability to save stopwatch sets to refer to at a later time.

Solutions I tried

1. ChatGPT 4o suggested solution: Define a separate StopwatchController which has the functions start(), stop(), reset() and contains a timer property.
Why is this not ideal: I find that I cannot globally control the stopwatches this way

2. Defining stopwatch control functions in StopwatchItemView, which is shown in ContentView in the form of

forEach(stopwatches) { stopwatch in
    StopwatchItemView(stopwatch: stopwatch)
}

Why this is not ideal: Unable to globally control stopwatches, harder to persist timer status/progress when app is quit

Code

Note: Including ContentView is not relevant as it still features the boilerplate code.

StopwatchController.swift

import Foundation
import SwiftData
import Combine

@Model
class StopwatchSet: Identifiable, ObservableObject {
    var id = UUID()
    var label: String
    var stopwatches: [StopwatchItem]
    
    init(id: UUID = UUID(), label: String, stopwatches: [StopwatchItem]) {
        self.id = id
        self.label = label
        self.stopwatches = stopwatches
    }
}

@Model
class StopwatchItem: Identifiable, ObservableObject {
    var id = UUID()
    var isRunning: Bool = false
    var label: String
    var timeElapsed: Double = 0.0
    var laps: [Lap] = []
    private var timer: Timer?
    
    init(id: UUID = UUID(), label: String, timeElapsed: Double) {
        self.id = id
        self.label = label
        self.timeElapsed = timeElapsed
    }
    
    func start() {
        isRunning = true
        timer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true) { [weak self] _ in
            self?.timeElapsed += 0.01
        }
    }
    
    func stop() {
        isRunning = false
        timer?.invalidate()
        timer = nil
    }
    
    func reset() {
        stop()
        timeElapsed = 0.0
    }
}

@Model
class Lap: Identifiable, ObservableObject {
    var id = UUID()
    var label: String
    var timeElapsed: String
    
    init(label: String, timeElapsed: String) {
        self.label = label
        self.timeElapsed = timeElapsed
    }
}

StopwatchItemView.swift

import SwiftUI
import SwiftData

struct StopwatchItemView: View {
    
    @State var stopwatch: StopwatchItem
    
    var body: some View {
        VStack {
            Text(formatTime(stopwatch: stopwatch, input: stopwatch.timeElapsed))
        }
    }
}

#Preview {
    StopwatchItemView(stopwatch: StopwatchItem(label: "Stopwatch", timeElapsed: 0.0))
}

New contributor

Ethan Lim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật