Post

Replies

Boosts

Views

Activity

Reply to drag and drop cards
This is my card data // // RouteList.swift // BikeComputer // // Created by Fabian Szukat on 18.08.24. // import Foundation import SwiftUI import UniformTypeIdentifiers class CardData: Observable, Transferable, Codable, Hashable{ var title: String var icon: String var text: String var height: CGFloat var width: CGFloat var warning: Bool var warningText: String private var id: UUID var localizedTitle: LocalizedStringKey { return LocalizedStringKey(title) } enum CodingKeys: String, CodingKey { case title case icon case height case width case warning case warningText case id case text } var localizedText: LocalizedStringKey { return LocalizedStringKey(text) } static func == (lhs: CardData, rhs: CardData) -> Bool { if rhs.hashValue == lhs.hashValue { return true } return false } required init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) id = try container.decode(UUID.self, forKey: .id) title = try container.decode(String.self, forKey: .title) text = try container.decode(String.self, forKey: .text) width = try container.decode(CGFloat.self, forKey: .width) height = try container.decode(CGFloat.self, forKey: .height) icon = try container.decode(String.self, forKey: .icon) warning = try container.decode(Bool.self, forKey: .warning) warningText = try container.decode(String.self, forKey: .warningText) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) try container.encode(title, forKey: .title) try container.encode(text, forKey: .text) try container.encode(width, forKey: .width) try container.encode(height, forKey: .height) try container.encode(icon, forKey: .icon) try container.encode(warning, forKey: .warning) try container.encode(warningText , forKey: .warningText) } init(){ self.id = UUID() self.title = "" self.icon = "" self.text = "" self.height = 100 self.width = 200 self.warning = false self.warningText = "" } public func hash(into hasher: inout Hasher) { return hasher.combine(id) } static var transferRepresentation: some TransferRepresentation { CodableRepresentation(contentType: .carddata) } } This is my view // // RouteList.swift // BikeComputer // // Created by Fabian Szukat on 18.08.24. // import SwiftUI import SwiftData struct RouteView: View { @Environment(\.modelContext) private var modelContext @Query(sort: \Route.name) var routes: [Route] var body: some View { NavigationSplitView { List{ if routes.isEmpty { Text("No Entries avaible") }else{ ForEach(routes) { route in NavigationLink{ RouteDetail(route: route) } label: { RouteRow(route: route) }.deleteDisabled(route.readonly).disabled(route.locked) }.onDelete(perform: { indexes in for index in indexes { let route = routes[index] modelContext.delete(route) } }) } } .toolbar { EditButton() } } detail: { Text("Select a Route") }.navigationTitle("Routen") } } #Preview { RouteView() } func addPointToRoute(loc: CLLocation ) { let point = Point() point.speed = loc.speed point.altitude = loc.altitude point.datetime = loc.timestamp point.speedAccuracy = loc.speedAccuracy point.course = loc.course point.horizontalAccuracy = loc.horizontalAccuracy point.verticalAccuracy = loc.verticalAccuracy let coordinate = Coordinates() coordinate.latidude = loc.coordinate.latitude coordinate.longitude = loc.coordinate.longitude //point.coordinate = coordinate /*if(self.point.altimeters.count > 0){ point.altimeters.append(self.point.altimeters.last ?? Altimeter()) }*/ if(recording && !pausedRecording ){ addpoint(point: point) } self.point = point updateCard(titel: "", text: "") } private func updateCard(titel: String, text:String){ for (index, element) in cards.enumerated() { cards[index].text = "text" } } ..``` I tried so much sorry
Sep ’24
Reply to no data from userdefaults
Hello, This is a simple environmental object. This Object has only two variables. a getter and init method I get only a warning: Conditional downcast from 'Data?' to 'Data' does nothing import Foundation class RouteList: ObservableObject { @Published var routes: [RouteObject] let saveKey = "SavedData" init() { if let data = UserDefaults.standard.data(forKey: saveKey) as? Data { print(data) if let decoded = try? JSONDecoder().decode([RouteObject].self, from: data) { routes = decoded return } } routes = [] } func save() { if let encoded = try? JSONEncoder().encode(routes) { UserDefaults.standard.set(encoded, forKey: saveKey) } } } protocol ProtocolName: ObservableObject { var routes: [RouteObject] { get } }
Apr ’24