I just made a small test app that uses SwiftData with CloudKit capability. I created a simple Book model as seen below. It looks like enums and structs when used with CloudKit capability all trigger this error:
'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release
I fixed the error by using genreRaw
String and using a computed property to use it in the app, but it popped back up after adding the ReadingProgress struct
Should I ignore the error and assume Apple still supports enums and codable structs when using SwiftData with CloudKit?
import SwiftData
@Model
class Book {
var title: String = ""
var author: String = ""
var genreRaw: String = Genre.fantasy.rawValue
var review: String = ""
var rating: Int = 3
var progress: ReadingProgress?
var genre: Genre {
get { Genre(rawValue: genreRaw) ?? Genre.fantasy }
set { genreRaw = newValue.rawValue }
}
init(title: String, author: String, genre: Genre, review: String, rating: Int, progress: ReadingProgress? = nil) {
self.title = title
self.author = author
self.genre = genre
self.review = review
self.rating = rating
self.progress = progress
}
}
struct ReadingProgress: Codable {
var currentPage: Int
var totalPages: Int
var isFinished: Bool
var percentComplete: Double {
guard totalPages > 0 else { return 0 }
return Double(currentPage) / Double(totalPages) * 100
}
}
enum Genre: String, Codable, CaseIterable {
case fantasy
case scienceFiction
case mystery
case romance
var displayName: String {
switch self {
case .fantasy:
return "Fantasy"
case .scienceFiction:
return "Science Fiction"
case .mystery:
return "Mystery"
case .romance:
return "Romance"
}
}
}