Post

Replies

Boosts

Views

Activity

Reply to Thread 1: "NSFetchRequest could not locate an NSEntityDescription for entity name 'Goal'"
From Apple DTS: This is an issue on the framework side – when an app uses a ModelContainer with multiple ModelConfiguration to manage multiple stores, only the model types specified in the first configuration are loaded, and that triggers the error. This issue should have been fixed in iOS 17.4, which is now beta 2. You can download the beta and give it a try. Feel free to follow up if the issue is still there. If you would like to support versions before 17.4, consider creating one container per configuration.
Feb ’24
Reply to Draw an apple using SwiftUI and path
Here is an example I've written a few month ago: import SwiftUI struct Apple: Shape { func path(in rect: CGRect) -> Path { Path { path in let width = rect.width let height = rect.height //Draw the apple's body path.move(to: CGPoint(x: width * 0.55, y: height * 0.31)) path.addCurve( to: CGPoint(x: width * 0.56, y: height * 0.96), control1: CGPoint(x: width * 0.85, y: height * 0.15), control2: CGPoint(x: width * 0.7, y: height * 1.1) ) path.addCurve( to: CGPoint(x: width * 0.55, y: height * 0.31), control1: CGPoint(x: width * 0.36, y: height * 1.15), control2: CGPoint(x: width * 0.24, y: height * 0.18) ) } } } struct AppleView: View { var body: some View { ZStack { Apple() .fill(.red) .frame(height: 210, alignment: .center) //Draw the leaf of the apple Ellipse() .fill(.green) .rotationEffect(.degrees(45)) .offset(x: 30, y: -56) .frame(width: 20, height: 50, alignment: .center) } } }
Dec ’22
Reply to Generics and AssociatedTypes
This should work class PersistantStorage<T: Codable & Identifiable> { func store(_ object: T) throws { } func objectFor(_ key: T.ID) -> T? { return nil } } You could also write it like this: class PersistantStorage<T> where T: Codable & Identifiable{ func store(_ object: T) throws { } func objectFor(_ key: T.ID) -> T? { return nil } }
Jun ’22
Reply to Keep face up the cards if equal
@Published var cards: [Card] = [ Card(content: "😀"), Card(content: "😀"), Card(content: "😘"), Card(content: "🥶"), Card(content: "😡"), Card(content: "🥶"), Card(content: "😘"), Card(content: "😡") ] Represents all your cards, in this case 8 cards in the array
Jan ’22
Reply to Keep face up the cards if equal
import SwiftUI struct Card: Identifiable{ var id = UUID() var content: String var isFacedUp: Bool = false var isMatched = false } class ViewModel: ObservableObject{ @Published var cards: [Card] = [ Card(content: "😀"), Card(content: "😀"), Card(content: "😘"), Card(content: "🥶"), Card(content: "😡"), Card(content: "🥶"), Card(content: "😘"), Card(content: "😡") ] func tapped(_ card: Card){ if let chosenIndex = cards.firstIndex(where: { $0.id == card.id}), !cards[chosenIndex].isFacedUp, !cards[chosenIndex].isMatched{ if let potenialMatcehIndex = cards.indices.filter({ cards[$0].isFacedUp}).oneAndOnly{ print("ONE") if cards[chosenIndex].content == cards[potenialMatcehIndex].content{ print("TWO") cards[potenialMatcehIndex].isMatched = true cards[chosenIndex].isMatched = true } cards[chosenIndex].isFacedUp = true } else{ print("THREE") cards.indices.forEach({cards[$0].isFacedUp = ($0 == chosenIndex)}) } } } } struct ContentView: View { let columns = [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())] @StateObject var viewModel = ViewModel() var body: some View { LazyVGrid(columns: columns) { ForEach(viewModel.cards) { card in RoundedRectangle(cornerRadius: 20) .frame(width: 70, height: 70) .foregroundColor(card.isFacedUp || card.isMatched ? Color(.systemIndigo) : .purple) .padding() .overlay(Text(card.content).font(.system(size: 30)).rotation3DEffect(Angle(degrees: card.isFacedUp || card.isMatched ? 180 : 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))).opacity(card.isFacedUp || card.isMatched ? 1 : 0)) .rotation3DEffect(card.isFacedUp || card.isMatched ? Angle(degrees: 180): Angle(degrees: 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))) .animation(.default, value: card.isFacedUp) .onTapGesture { viewModel.tapped(card) } } } } } extension Array{ var oneAndOnly: Element?{ if self.count == 1{ return self.first } else{ return nil } } }
Jan ’22
Reply to Need a help with my app
Try this: import SwiftUI struct Card: Identifiable{ var id = UUID() var content: String var isFacedUp: Bool = false var isMatched = false } class ViewModel: ObservableObject{ @Published var cards: [Card] = [ Card(content: "😀"), Card(content: "😀"), Card(content: "😘"), Card(content: "🥶"), Card(content: "😡"), Card(content: "🥶"), Card(content: "😘"), Card(content: "😡") ] func tapped(_ card: Card){ if let chosenIndex = cards.firstIndex(where: { $0.id == card.id}), !cards[chosenIndex].isFacedUp, !cards[chosenIndex].isMatched{ if let potenialMatcehIndex = cards.indices.filter({ cards[$0].isFacedUp}).oneAndOnly{ print("ONE") if cards[chosenIndex].content == cards[potenialMatcehIndex].content{ print("TWO") cards[potenialMatcehIndex].isMatched = true cards[chosenIndex].isMatched = true } cards[chosenIndex].isFacedUp = true } else{ print("THREE") cards.indices.forEach({cards[$0].isFacedUp = ($0 == chosenIndex)}) } } } } struct ContentView: View { let columns = [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())] @StateObject var viewModel = ViewModel() var body: some View { LazyVGrid(columns: columns) { ForEach(viewModel.cards) { card in RoundedRectangle(cornerRadius: 20) .frame(width: 70, height: 70) .foregroundColor(card.isFacedUp || card.isMatched ? Color(.systemIndigo) : .purple) .padding() .overlay(Text(card.content).font(.system(size: 30)).rotation3DEffect(Angle(degrees: card.isFacedUp || card.isMatched ? 180 : 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))).opacity(card.isFacedUp || card.isMatched ? 1 : 0)) .rotation3DEffect(card.isFacedUp || card.isMatched ? Angle(degrees: 180): Angle(degrees: 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))) .animation(.default, value: card.isFacedUp) .onTapGesture { viewModel.tapped(card) } } } } } extension Array{ var oneAndOnly: Element?{ if self.count == 1{ return self.first } else{ return nil } } }
Jan ’22
Reply to .onDelete resets NSPredicate (Core Data, SwiftUI)
I found a solution. @FetchRequest( entity: LifetimeInputs.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: true)], predicate: nil ) var lifetimeInputsModel: FetchedResults<LifetimeInputs> Becomes this (in my case) @FetchRequest var lifetimeInputs: FetchedResults<LifetimeInputs> init(nsPredicate: NSPredicate, sortDescriptors: [NSSortDescriptor]) { let entity = LifetimeInputs.entity() let fetchRequest = FetchRequest<LifetimeInputs>(entity: entity, sortDescriptors: sortDescriptors, predicate: nsPredicate, animation: .default) self._lifetimeInputs = fetchRequest } The nsPredicate and the sortDescriptors must be passed to a parent view and you have to change/update (if you need to) them in that parent view. Hope it works also for you
Jan ’22
Reply to Flip animation in SwiftUI
I think that the solution is in your previous thread, anyway here is the solution: struct ContentView: View { @State private var flip: Bool = false var body: some View { Group { Button { withAnimation { flip.toggle() } } label: { Rectangle() .frame(width: 140, height: 170) .foregroundColor(Color(.systemTeal)) .cornerRadius(20) .rotation3DEffect(.degrees(flip ? 180 : 0), axis: (x: 0, y: 1, z: 0)) } } } }
Jan ’22
Reply to Add Text to an animated object
struct ContentView: View { @State private var flipped = false var body: some View { Group { RoundedRectangle(cornerRadius: 20) .frame(width: 140, height: 170) .foregroundColor(flipped ? .red : .orange) .padding() .overlay(Text("Hello, world!").rotation3DEffect(Angle(degrees: flipped ? 180 : 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))).opacity(flipped ? 1 : 0)) .rotation3DEffect(flipped ? Angle(degrees: 180): Angle(degrees: 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))) .animation(.default, value: flipped) .onTapGesture { flipped.toggle() } } } }
Jan ’22
Reply to Add Text to an animated object
struct ContentView: View { @State private var flipped = false var body: some View { Group { RoundedRectangle(cornerRadius: 20) .frame(width: 140, height: 170) .foregroundColor(flipped ? .yellow : .purple) .padding() .overlay(Text("Hello, world!").rotation3DEffect(Angle(degrees: flipped ? 180 : 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))).opacity(flipped ? 1 : 0)) .rotation3DEffect(flipped ? Angle(degrees: 180): Angle(degrees: 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))) .animation(.default, value: flipped) .onTapGesture { flipped.toggle() } } } } Is this ok?
Jan ’22
Reply to Add Text to an animated object
Or maybe this: struct ContentView: View { @State private var flipped = false var body: some View { Group { ZStack{ RoundedRectangle(cornerRadius: 20) .frame(width: 140, height: 170) .foregroundColor(flipped ? .red : .orange) .padding() Text("Hello, world!") .rotation3DEffect(.degrees(flipped ? 180 : 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))) } .rotation3DEffect(Angle(degrees: flipped ? 180 : 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0))) .onTapGesture { withAnimation(.linear(duration: 1)) { flipped.toggle() } } } } }
Jan ’22