Post not yet marked as solved
I'm developing an application using SwiftUI and SwiftData. The app includes a pricing section, and I'd like it to have an initial default value for pricing. Additionally, when updating the app on the App Store, I also want to update the prices. However, I'd like users to have the option to create their own prices if they don't want to use the ones I suggest. I want to save these prices obtained from the user because I'll be using these values for some operations later on.
I'm wondering how to achieve this. Should I use SwiftData or UserDefaults for managing the data, or should I save the prices to a JSON file? If I opt for saving to a JSON file, would it be possible to update the JSON file when updating the app? Please feel free to ask any questions. Thank you in advance for your responses.
Post not yet marked as solved
Hello everyone, I hope you are well. I have a question about .environment. I have an observable viewModel which has some functions and publishing value. I'm observing this viewModel in only 2 views but I'm using viewModel functions in every view. Should I use it (.environment). if I should use it, should I use this environment macro (@Environment(ViewModel.Self) var vm) for only functions in view? Thank you so much.
I'm getting this error : Picker: the selection "3" is invalid and does not have an associated tag, this will give undefined results. Because new brand doesn't have 3 values and .onChange modifier is not working fast enough. Thanks for your help.
Picker("Marka", selection: $brandIndex) {
Text("Seçin").tag(0)
ForEach(cars.indices, id: \.self) {
Text(cars[$0].brand).tag($0 + 1)
}
}
.onChange(of: brandIndex) {
if modelIndex != 0 {
modelIndex = 0
}
}
Picker("Model", selection: $modelIndex) {
Text("Seçin").tag(0)
if brandIndex != 0 {
let _ = print(modelIndex) // I'm getting first 3 then 0. And I'm getting error.
ForEach(cars[brandIndex - 1].models.indices, id: \.self) {
Text(cars[brandIndex - 1].models[$0])
.tag($0 + 1)
}
}
}
Post not yet marked as solved
I'm getting error when I try to delete my kayit item. And the line causing the error is given below. When I removed this line everything is OK. I can delete my kayit item.
@Environment(\.modelContext) private var modelContext
@Environment(\.dismiss) private var dismiss
@Bindable var kayit: Kayit
@FocusState private var focusedField: Field?
var body: some View {
Form {
Section {
Text(kayit.ozellikleri)
Text(kayit.girisKaydi.baglanmaNedeni) // This line gives error
}
}
.navigationTitle("Edit Registery")
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button {
modelContext.delete(kayit)
try? modelContext.save()
dismiss()
} label: {
Image(systemName: "trash")
.foregroundStyle(.red)
}
}
}
}
When I use giriskaydi in anywhere it gives error. When I remove I can delete my item.
This is my model:
import Foundation
import SwiftData
@Model
class Kayit {
var aktifKayit = true
var plaka: String
var marka: String
var model: String
var ozellikleri: String
var kategori: Kategori
var girisKaydi: GirisKaydi
var cikisKaydi: CikisKaydi
init(aktifKayit: Bool = true, plaka: String, marka: String, model: String, ozellikleri: String, kategori: Kategori, girisKaydi: GirisKaydi, cikisKaydi: CikisKaydi) {
self.aktifKayit = aktifKayit
self.plaka = plaka
self.marka = marka
self.model = model
self.ozellikleri = ozellikleri
self.kategori = kategori
self.girisKaydi = girisKaydi
self.cikisKaydi = cikisKaydi
}
}
@Model
class GirisKaydi {
var baglayanKurum: String
var baglanmaNedeni: String
var girisTarihi: Date
var personel: String
var cekiciUcreti: Double
init(baglayanKurum: String, baglanmaNedeni: String, girisTarihi: Date, personel: String, cekiciUcreti: Double) {
self.baglayanKurum = baglayanKurum
self.baglanmaNedeni = baglanmaNedeni
self.girisTarihi = girisTarihi
self.personel = personel
self.cekiciUcreti = cekiciUcreti
}
}
@Model
class CikisKaydi {
var teslimAd: String
var teslimSoyad: String
var adres: String
var telefon: String
var dayanak: String
var cikisTarihi: Date
var aciklama: String
init(teslimAd: String, teslimSoyad: String, adres: String, telefon: String, dayanak: String, cikisTarihi: Date, aciklama: String) {
self.teslimAd = teslimAd
self.teslimSoyad = teslimSoyad
self.adres = adres
self.telefon = telefon
self.dayanak = dayanak
self.cikisTarihi = cikisTarihi
self.aciklama = aciklama
}
}
And the error:
@Model
class Kayit {
var aktifKayit = true
var plaka: String
var marka: String
var model: String
var ozellikleri: String
var kategori: Kategori
var girisKaydi: GirisKaydi
{
@storageRestrictions(accesses: _$backingData, initializes: _girisKaydi)
init(initialValue) {
_$backingData.setValue(forKey: \.girisKaydi, to: initialValue)
_girisKaydi = _SwiftDataNoType()
}
get {
_$observationRegistrar.access(self, keyPath: \.girisKaydi)
return self.getValue(forKey: \.girisKaydi) // Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1cb45f898)
}
set {
_$observationRegistrar.withMutation(of: self, keyPath: \.girisKaydi) {
self.setValue(forKey: \.girisKaydi, to: newValue)
}
}
}
var cikisKaydi: CikisKaydi
init(aktifKayit: Bool = true, plaka: String, marka: String, model: String, ozellikleri: String, kategori: Kategori, girisKaydi: GirisKaydi, cikisKaydi: CikisKaydi) {
self.aktifKayit = aktifKayit
self.plaka = plaka
self.marka = marka
self.model = model
self.ozellikleri = ozellikleri
self.kategori = kategori
self.girisKaydi = girisKaydi
self.cikisKaydi = cikisKaydi
}
And is says CoreData: warning: Warning: Dropping Transactions prior to 52 for Persistent History in console. Thank you so much.