Posts

Post not yet marked as solved
2 Replies
1.1k Views
I am using SwiftData in a SwiftUI view called DeckView and I'm adding a AddDeck function but let deck = Deck() is returning 'Missing argument for parameter 'backingData' in call' Here is the full code: DeckView import SwiftUI import SwiftData struct DeckView: View { @State private var showingSheet = false @Environment(\.modelContext) var modelContext @Query var decks: [Deck] @State private var path = [Deck]() var body: some View { NavigationStack(path: $path) { ZStack { Color.background .ignoresSafeArea() List { ForEach(decks) { deck in NavigationLink(value: deck) { HStack { VStack(alignment: .leading) { Text(deck.name) HStack { Image(systemName: "square.3.layers.3d.top.filled") Text(deck.cards) } .padding(.top) } } } } .onDelete(perform: deleteDeck) .listRowBackground(Color(.secondaryC)) } .scrollContentBackground(.hidden) } .foregroundColor(Color(.text)) .navigationTitle("Decks") .navigationDestination(for: Deck.self, destination: EditDeckView.init) .toolbar { ToolbarItem(placement: .topBarTrailing) { Image(systemName: "plus") .onTapGesture { showingSheet.toggle() addSamples() } } } .sheet(isPresented: $showingSheet) { CreateDeckSheet() } } } func addSamples() { let swiftd = Deck(name: "SwiftData", cards: "20") let swifty = Deck(name: "Swift", cards: "21") let swiftyui = Deck(name: "SwiftUI", cards: "32") modelContext.insert(swiftd) modelContext.insert(swifty) modelContext.insert(swiftyui) } func addDeck() { let deck = Deck() modelContext.insert(deck) path = [deck] } func deleteDeck(_ indexSet: IndexSet) { for index in indexSet { let deck = decks[index] modelContext.delete(deck) } } } #Preview { DeckView() } Data Model import Foundation import SwiftData @Model class Deck { var name: String var cards: String init(name: String, cards: String) { self.name = name self.cards = cards } }
Posted
by Jt10594.
Last updated
.
Post not yet marked as solved
1 Replies
875 Views
Problem I was using swift data on my project in what I thought was a not very complex data model and then after I added the delete function it gave me this error and crashed the simulator every time. DataModel import Foundation import SwiftData @Model class Deck { var name: String var cards: String init(name: String, cards: String) { self.name = name self.cards = cards } } And this is where I was fetching the data. DeckView import SwiftUI import SwiftData struct DeckView: View { @State private var showingSheet = false @Environment(\.modelContext) var modelContext @Query var decks: [Deck] var body: some View { NavigationStack { ZStack { Color.background .ignoresSafeArea() List { ForEach(decks) { deck in HStack { VStack(alignment: .leading) { Text(deck.name) Text(deck.cards) } Spacer() Button { // Action } label: { Image(systemName: "ellipsis.circle") } } } .onDelete(perform: deleteDeck) } .scrollContentBackground(.hidden) } .navigationTitle("Decks") .navigationBarTitleDisplayMode(.automatic) .toolbar { ToolbarItem(placement: .topBarTrailing) { Image(systemName: "plus") .onTapGesture { // showingSheet.toggle() addSamples() } } } .sheet(isPresented: $showingSheet) { NewDeckSheet() } } } func addSamples() { let swiftd = Deck(name: "SwiftData", cards: "20") let swifty = Deck(name: "Swift", cards: "21") let swiftyui = Deck(name: "SwiftUI", cards: "32") modelContext.insert(swiftd) modelContext.insert(swifty) modelContext.insert(swiftyui) } func deleteDeck(_ indexSet: IndexSet) { for index in indexSet { let deck = decks[index] modelContext.delete(deck) } } } What the console returns Deck = "(<NSEntityDescription: 0x600002c88790>) name Deck, managedObjectClassName NSManagedObject, renamingIdentifier Deck, isAbstract 0, superentity name (null), properties {\n cards = \"(<NSAttributeDescription: 0x6000035a9080>), name cards, isOptional 0, isTransient 0, entity Deck, renamingIdentifier cards, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 300 , attributeValueClassName NSNumber, defaultValue (null), preservesValueInHistoryOnDeletion NO, allowsCloudEncryption NO\";\n name = \"(<NSAttributeDescription: 0x6000035a9100>), name name, isOptional 0, isTransient 0, entity Deck, renamingIdentifier name, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null), preservesValueInHistoryOnDeletion NO, allowsCloudEncryption NO\";\n}, subentities {\n}, userInfo {\n}, versionHashModifier (null), uniquenessConstraints (\n)"; }, fetch request templates { }, destinationModel=(<NSManagedObjectModel: 0x6000038a69e0>) isEditable 0, entities { Deck = "(<NSEntityDescription: 0x600002c84420>) name Deck, managedObjectClassName Deck, renamingIdentifier Deck, isAbstract 0, superentity name (null), properties {\n cards = \"(<NSAttributeDescription: 0x60000359f680>), name cards, isOptional 0, isTransient 0, entity Deck, renamingIdentifier cards, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null), preservesValueInHistoryOnDeletion NO, allowsCloudEncryption NO\";\n name = \"(<NSAttributeDescription: 0x60000359f580>), name name, isOptional 0, isTransient 0, entity Deck, renamingIdentifier name, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null), preservesValueInHistoryOnDeletion NO, allowsCloudEncryption NO\";\n}, subentities {\n}, userInfo {\n}, versionHashModifier (null), uniquenessConstraints (\n)"; }, fetch request templates { }, reason=Can't find or automatically infer mapping model for migration, NSUnderlyingError=0x60000156cb70 {Error Domain=NSCocoaErrorDomain Code=134190 "(null)" UserInfo={entity=Deck, property=cards, reason=Source and destination attribute types are incompatible}}} And then: SwiftData/ModelContainer.swift:144: Fatal error: failed to find a currently active container for Deck And: Failed to find any currently loaded container for Deck) Any help would be appreciated, thank you!
Posted
by Jt10594.
Last updated
.
Post not yet marked as solved
1 Replies
408 Views
Hello, I'm a little new to Swift and very new to SwiftData. I'm making a flashcard app in which the decks of flashcards are displayed in a view. I've created a reusable component for the decks in which I want to use SwiftData to store the Title date of creation and eventually number of cards. So far I'm just trying to get the title. Here is the component: Deck Component import SwiftUI import SwiftData struct DeckRow: View { @State private var isPressed = false @State private var isToggled = false @Environment(\.modelContext) var modelContext @Query var decks: [Deck] var body: some View { ZStack { RoundedRectangle(cornerRadius: 20) .frame(height: 99) .padding(.horizontal) .foregroundStyle(Color.primaryC) .offset(y: 8) HStack(alignment: .top) { VStack(alignment: .leading, spacing: 20) { HStack { Text(decks.name) .font(.title2) .bold() Text("2w") .font(.subheadline) .fontWeight(.light) } HStack { Image(systemName: "square.3.layers.3d.top.filled") Text("20 Cards") } } Spacer() Button { } label: { Image(systemName: "ellipsis.circle") .font(.title3) .bold() } } .padding() .foregroundStyle(Color.text) .background(Color.secondaryC) .cornerRadius(15) .padding() .offset(y: isPressed ? 8 : 0) .gesture( DragGesture(minimumDistance: 0) .onChanged { _ in withAnimation(.linear(duration: 0.5)) { isToggled.toggle() } isPressed = true } .onEnded { _ in withAnimation(.easeInOut) { isPressed = false } } ) } } } But, Text(decks.name) keeps returningValue of type '[Deck]' has no member 'name' even though it does. Here is the data model: Data Model import Foundation import SwiftData @Model class Deck { var name: String var cards: Int var creationDate: Date init(name: String = "", cards: Int = 0, creationData: Date = Date()) { self.name = name self.cards = cards self.creationDate = creationData } } Any help or tips would be greatly appreciated. Thank you!
Posted
by Jt10594.
Last updated
.
Post marked as solved
1 Replies
471 Views
On the swift student challenge page it says "Your app playground must be built with and run on Swift Playgrounds 4.2.1 or later (requires iPadOS 16 or macOS 13) or Xcode 14 on macOS 13." Could I build it with Xcode in a swift.pm file but ask for it to be viewed on iPhone?
Posted
by Jt10594.
Last updated
.
Post marked as solved
1 Replies
1k Views
Hello, i am working on my project for the Swift Student Challenge and it came to my attention that the project must be built using Swift Playgrounds. I have run into an issue where I want to add multiple views to my project but it seems i am unable. How am i supposed to do this?
Posted
by Jt10594.
Last updated
.