Posts

Post not yet marked as solved
4 Replies
375 Views
Hello If I have this array: var objects = [ Object(value: 100, date: Date(), imageTemplate: "30"), Object(value: 200, date: Date(), imageTemplate: "20"), Object(value: 400, date: Date() + 84000, imageTemplate: "10") ] How can I count how many different dates are in the array using NSPredicate? In this case it should return 2. Thank You!
Posted
by Jad-T.
Last updated
.
Post not yet marked as solved
1 Replies
1.3k Views
Hello I am using a SwiftUI @FetchRequest to displays Core Data items, one of the properties of the entity is date, and I want to filter items by today's Date, this is the @FetchRequest: @FetchRequest( entity: Book.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Book.date, ascending: true)], predicate: NSPredicate(format: "date == %@"), animation: .default) var books: FetchedResults<Book> How do I complete the NSPredicate to make it work? (I know that there are no arguments in the predicate yet) Thank You!
Posted
by Jad-T.
Last updated
.
Post not yet marked as solved
0 Replies
378 Views
Hello @FetchRequest( entity: Book.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Book.entity.date, ascending: true)] ) var books: FetchedResults<Book.entity> How can I get notified when anything in book changes without using .onRecevie(books.publisher){ _ in .... } And is there any way to use .onChange modifier with books? Thank You!
Posted
by Jad-T.
Last updated
.
Post not yet marked as solved
0 Replies
325 Views
Hello @FetchRequest( entity: Book(), sortDescriptors: [] ) var books: FetchedResults<Book> How can I get notified when books changes? I was thinking about putting this... .onReceive(NotificationCenter.default.publisher(for: .NSManagedObjectContextDidSave), perform: { _ in}) ... in my main View but this would notify me if anything gets saved and that is not what I want. I want to get notified just if a certain FetchRequest changes (in this case books)? And in another thread I was told to use... .onRecevie(books.publisher){ _ in .... } ...however using this code publishes each member of the sequence as a separate element, I want it to publish just once? Thank You!
Posted
by Jad-T.
Last updated
.
Post not yet marked as solved
2 Replies
308 Views
Hello @FetchRequest( entity: Book(), sortDescriptors: [] ) var books: FetchedResults<Book> How can I get notified when books changes? I was thinking about putting this... .onReceive(NotificationCenter.default.publisher(for: .NSManagedObjectContextDidSave), perform: { _ in}) ... in my main View but this would notify me if anything gets saved and that is not what I want. I want to get notified just if a certain FetchRequest changes (in this case books)? How can I do that? Thank You!
Posted
by Jad-T.
Last updated
.
Post not yet marked as solved
0 Replies
266 Views
Hello In my app I have a list of items in a ForEach and at the end of the ForEach there is a .onDelete. (This is in a View that I called AllHistoryView) The items are represented by a CoreData NSManagedObject that I called LifetimeInputs with 3 properties: date: Date, imageTemplate: String, valori: Double The problem is that, when I add (in a different View) an item with the date of tomorrow and then I add another to item with the date of today and then go to the AllHistoryView where there are all the items and delete slowly the item with date of tomorrow (especially at the end of the swipe action) instead of deleting the item that I swiped it deletes the on before it, how can I solve this problem? Here is the AllHistoryView code: import SwiftUI import CoreData struct AllHistoryView: View { @Environment(\.managedObjectContext) var viewContext @FetchRequest(entity: LifetimeInputs.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: true)], animation: .default) var lifetimeInputsModel: FetchedResults<LifetimeInputs> @State private var text: String = "" @State private var ascending: Bool = true @State var sortDescriptor: NSSortDescriptor = NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: true) @Environment(\.dynamicTypeSize) var dynamicTypeSize var size: CGFloat{ if UIDevice.current.userInterfaceIdiom == .phone { switch dynamicTypeSize { case .xSmall: return 11 case .small: return 13 case .medium: return 15 case .large: return 17 case .xLarge: return 19 case .xxLarge: return 21 case .xxxLarge: return 23 default: return 23 } } else { switch dynamicTypeSize { case .xSmall: return 13 case .small: return 15 case .medium: return 17 case .large: return 19 case .xLarge: return 21 case .xxLarge: return 23 case .xxxLarge: return 25 case .accessibility1: return 27 case .accessibility2: return 29 default: return 29 } } } var body: some View { theView() } @ViewBuilder func theView() -> some View{ NavigationView{ if !lifetimeInputsModel.isEmpty{ List{ SectionList(sortDescripter: sortDescriptor, text: $text) .environment(\.managedObjectContext, viewContext) .onChange(of: ascending, perform: { _ in if ascending { withAnimation { sortDescriptor = NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: true) } } else { withAnimation { sortDescriptor = NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: false) } } }) } .searchable(text: $text, placement: .navigationBarDrawer, prompt: "Quantity or date".localized()) .navigationBarTitle("History", displayMode: .inline) .toolbar{ ToolbarItem(placement: .automatic) { Image(systemName: "arrow.up.arrow.down.circle") .foregroundColor(.primary) .font(.system(size: size)) .rotation3DEffect(.degrees(ascending ? 0 : 180), axis: (x: 1, y: 0, z: 0)) .opacity(0.5) .onTapGesture { ascending.toggle() } } } } else{ VStack{ Text("No Data".localized()) .font(.largeTitle) .fontWeight(.semibold) .foregroundColor(.secondary) } .padding(.bottom) .navigationBarTitle("History", displayMode: .inline) } } } } struct SectionList: View { @Environment(\.managedObjectContext) var viewContext @FetchRequest var lifetimeInputsModel: FetchedResults<LifetimeInputs> @Binding var text: String init(sortDescripter: NSSortDescriptor, text: Binding<String>) { let request: NSFetchRequest<LifetimeInputs> = LifetimeInputs.fetchRequest() request.sortDescriptors = [sortDescripter] _lifetimeInputsModel = FetchRequest<LifetimeInputs>(fetchRequest: request) self._text = text } @FetchRequest(entity: Limit.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Limit.date, ascending: true)], animation: .default) var limit: FetchedResults<Limit> @Environment(\.dynamicTypeSize) var dynamicTypeSize var size: CGFloat{ if UIDevice.current.userInterfaceIdiom == .phone { switch dynamicTypeSize { case .xSmall: return 11 case .small: return 13 case .medium: return 15 case .large: return 17 case .xLarge: return 19 case .xxLarge: return 21 case .xxxLarge: return 23 default: return 23 } } else { switch dynamicTypeSize { case .xSmall: return 13 case .small: return 15 case .medium: return 17 case .large: return 19 case .xLarge: return 21 case .xxLarge: return 23 case .xxxLarge: return 25 case .accessibility1: return 27 case .accessibility2: return 29 default: return 29 } } } @StateObject var lifeTimeInputsViewModel = LifeTimeInputsViewModel() var body: some View { Section{ ForEach(lifetimeInputsModel.filter { text.isEmpty || "\($0)".contains(text) }){ lifetimeInputs in HStack{ Text("\(lifetimeInputs.valori, specifier: format(unita: !limit.isEmpty ? limit[0].unita ?? ml : ml)) \(!limit.isEmpty ? limit[0].unita ?? ml: ml)") .font(.system(size: size)) Spacer() Text("\(dateFormatter.string(from: lifetimeInputs.date ?? Date()))") .font(.system(size: size)) } } .onDelete(perform: { offsets in lifeTimeInputsViewModel.deleteItems(offsets: offsets) }) } header: { Text("History".localized()).font(.system(size: size - 4)) } } } Thank You very much!
Posted
by Jad-T.
Last updated
.
Post marked as solved
3 Replies
493 Views
Hello I created a custom shape in SwiftUI and I am trying to rotate it around a circle, but it works just on the top part of the circle, can you help me make it rotate exactly around the circle? (And also can I get the same effect using radians? How?) Here is the code: import SwiftUI struct MyGameView: View { @State private var degress: Double = 0 let timer = Timer.publish(every: 0.05, on: .main, in: .common).autoconnect() var body: some View { VStack{ ZStack{ Circle() .frame(width: 80) ZStack{ Circle() .stroke(lineWidth: 1) .frame(width: 300) BallonShape() .scaledToFit() .scaleEffect(0.2) .foregroundColor(.red) .rotationEffect(.degrees(degress), anchor: .bottom) .offset(x: 0, y: -170) } } } .onReceive(timer) { input in withAnimation(.easeIn(duration: 0.05).speed(10)){ degress += 1 } } } } struct BallonShape: Shape { func path(in rect: CGRect) -> Path { Path { path in path.move(to: CGPoint(x: rect.midX, y: (rect.maxY + rect.midY) / 2)) path.addCurve(to: CGPoint(x: rect.midX, y: rect.minY), control1: CGPoint(x: (rect.midX + rect.minX) / 2, y: rect.minY), control2: CGPoint(x: (rect.midX + rect.minX) / 2, y: rect.minY)) path.addCurve(to: CGPoint(x: rect.midX, y: (rect.maxY + rect.midY) / 2), control1: CGPoint(x: (rect.midX + rect.maxX) / 2, y: rect.minY), control2: CGPoint(x: (rect.midX + rect.maxX) / 2, y: rect.minY)) } } } Thank You very much!
Posted
by Jad-T.
Last updated
.
Post marked as solved
4 Replies
1.4k Views
Hello I wanted to know why when I print(Date()) the date is printed but two hours before? Example If I print(Date()) and the actual date is 2021-09-10 22:33:41 +0000, it prints 2021-09-10 20:33:41 +0000 instead of 2021-09-10 22:33:41 +0000. Why does this happen? Thank You!
Posted
by Jad-T.
Last updated
.
Post not yet marked as solved
1 Replies
321 Views
Hello In my previous post I asked: https://developer.apple.com/forums/thread/689720 (Read my previous post before continuing, please) My question now is, if I compare Dates between each other will they be formatted or not, and how would I fix this? Thank You
Posted
by Jad-T.
Last updated
.
Post marked as solved
1 Replies
374 Views
Hello I was trying to use @FetchRequest in a class, but found it doesn't work, so I was wondering if there was a way to use something that works like @FetchRequest in a class? Thank You!
Posted
by Jad-T.
Last updated
.
Post marked as solved
1 Replies
930 Views
Hello I am using matched Geometry Effect to make animations and transitions, the problem is that when I press to start the animation, the object being animated, in this case Text, is duplicated during the transition, and then when I press again to get it back to its original position, no animation takes place, how can I fix it. Here is the code: struct ContentView: View { @StateObject var numberViewModel = NumberViewModel() @Namespace var animation var body: some View { GeometryReader { geo in NavigationView{ ZStack { ScrollView{ LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())]) { ForEach(numbers){ number in NumberView(numberViewModel: numberViewModel, animation: animation, number: number) .onTapGesture { withAnimation(.easeInOut(duration: 1)){ numberViewModel.selected = number numberViewModel.tapped = true } } } } } if numberViewModel.tapped{ NumberTappedView(animation: animation, numberViewModel: numberViewModel) .position( x: geo.frame(in:.global).midX, y: geo.frame(in:.global).midY ) .onTapGesture { withAnimation(.easeInOut(duration: 1)){ numberViewModel.selected = Number(number: 0) numberViewModel.tapped = false } } } } } } } } struct NumberView: View { @ObservedObject var numberViewModel: NumberViewModel var animation: Namespace.ID var number: Number var body: some View{ GroupBox{ if !(numberViewModel.selected.number == number.number){ Text("\(number.number)") .font(.largeTitle) .frame(width: 100, height: 100, alignment: .center) .matchedGeometryEffect(id: number.number, in: animation) } } } } struct Number: Identifiable { var id = UUID() var number: Int } var numbers: [Number] = [ Number(number: 1), Number(number: 2) ] struct NumberTappedView: View { var animation: Namespace.ID @ObservedObject var numberViewModel: NumberViewModel var body: some View{ GroupBox{ Text("\(numberViewModel.selected.number)") .font(.largeTitle) .frame(width: 200, height: 200, alignment: .center) .matchedGeometryEffect(id: numberViewModel.selected.number, in: animation) } } } class NumberViewModel: ObservableObject { @Published var selected: Number = Number(number: 0) @Published var tapped: Bool = false } Thank You!
Posted
by Jad-T.
Last updated
.
Post marked as solved
3 Replies
480 Views
Hello I noticed that in my app, when I add data (I am using Core Data) the size of the app increases, as expected, the problem occurs when I delete data, the size of the app remains unchanged and sometimes increases, I thought there was an error in the my code and so I created, from scratch, a project for iOS with SwiftUI and Core Data enabled (the default template that Xcode provides) and also with the SwiftUI & Core Data default app the same problem happens. Is there a way to fix it or is there an explanation for this? Thank you!
Posted
by Jad-T.
Last updated
.
Post marked as solved
3 Replies
932 Views
Hello I want to be able to save Date in @AppStorage, and it works however I was wondering what was the difference between these two extensions, which one is better and why? extension Date: RawRepresentable { static var dateFormatter: DateFormatter = { let formatter = DateFormatter() formatter.dateStyle = .long return formatter }() public var rawValue: String { Date.dateFormatter.string(from: self) } public init?(rawValue: String) { self = Date.dateFormatter.date(from: rawValue) ?? Date() } } and extension Date: RawRepresentable { private static let formatter = ISO8601DateFormatter() public var rawValue: String { Date.formatter.string(from: self) } public init?(rawValue: String) { self = Date.formatter.date(from: rawValue) ?? Date() } } Thank You!
Posted
by Jad-T.
Last updated
.