Post

Replies

Boosts

Views

Activity

Problems with ForEach, SwiftUI
Hello I am getting a lot of errors in ForEach and I don't know why at all. Here is the code: The error is at line 12 import SwiftUI import Combine struct ContentView: View { @EnvironmentObject var networkController: NetworkControllerItalia var body: some View { Form{ TextField("Input city name", text: $networkController.cityName) Section { ForEach(networkController.users.weather, id: \.self){ user in } } } } } class NetworkControllerItalia: ObservableObject { private var can: AnyCancellable? @Published var cityName: String = "" @Published var users = [UserItalia(weather: Weather())] init(cityName: String) { self.cityName = cityName let url = URL(string: "http://api.openweathermap.org/data/2.5/weather?q=\(cityName)&appid=")! self.can = URLSession.shared.dataTaskPublisher(for: url) .map { $0.data } .decode(type: [UserItalia].self, decoder: JSONDecoder()) .eraseToAnyPublisher() .receive(on: DispatchQueue.main) .sink(receiveCompletion: {completion in print(completion) }, receiveValue: { users in self.users = users }) } } struct UserItalia: Decodable, Hashable{ var weather: Weather } struct Weather: Decodable, Hashable { var main: String? } Thank you
3
0
2.7k
Apr ’21
Errors in ForEach SwiftUI
Hello I get errors in ForEach that I don't know how to solve: Generic struct 'ForEach' requires that 'Main' conform to 'RandomAccessCollection' Unable to infer type of a closure parameter 'user' in the current context Error at line 30 - 31 Code: // // ContentView.swift // ARWeather // // Created by Jad Taljabini on 08/04/21. // import SwiftUI import Combine struct ContentView: View { @EnvironmentObject var networkController: NetworkControllerItalia var body: some View { NavigationView { Form{ TextField("Input city name", text: $networkController.cityName, onEditingChanged: { te_xt in networkController.url = URL(string: "https://api.openweathermap.org/data/2.5/weather?q=\(networkController.cityName)&appid=") ?? URL(string: "https://www.apple.com")! networkController.fun() }, onCommit: { withAnimation{ networkController.url = URL(string: "https://api.openweathermap.org/data/2.5/weather?q=\(networkController.cityName)&appid=") ?? URL(string: "https://www.apple.com")! networkController.fun() } }) Section { ForEach(networkController.users.weather ?? [], id: \.self){ user in Text(user.main ?? "") } ForEach(networkController.users.main ?? Main(temp: 0), id: \.self){ user in Text("\(user.temp)") } } } } } } class NetworkControllerItalia: ObservableObject { private var can: AnyCancellable? @Published var cityName: String = "" @Published var users = UserItalia(weather: []) var url = URL(string: "https://www.apple.com")! func fun(){ self.can = URLSession.shared.dataTaskPublisher(for: url) .map { $0.data } .decode(type: UserItalia.self, decoder: JSONDecoder()) .eraseToAnyPublisher() .receive(on: DispatchQueue.main) .sink(receiveCompletion: {completion in print(completion) }, receiveValue: { users in self.users = users }) }//Funzione che riempe users di dati da internet } struct UserItalia: Decodable, Hashable{ var weather: [Weather]? var main: Main? } struct Weather: Decodable, Hashable { var main: String? } struct Main: Decodable, Hashable { var temp: Float } The JSON API is like this: { "coord": { "lon": -0.1257, "lat": 51.5085 }, "weather": [ { "id": 801, "main": "Clouds", "description": "few clouds", "icon": "02d" } ], "base": "stations", "main": { "temp": 285.45, "feels_like": 283.96, "temp_min": 284.82, "temp_max": 285.93, "pressure": 1021, "humidity": 47 }, "visibility": 10000, "wind": { "speed": 5.66, "deg": 220 }, "clouds": { "all": 20 }, "dt": 1617888145, "sys": { "type": 1, "id": 1414, "country": "GB", "sunrise": 1617859180, "sunset": 1617907473 }, "timezone": 3600, "id": 2643743, "name": "London", "cod": 200 } Thank you
3
0
3.6k
Apr ’21
CloudKit and CoreData synchronization
Hello I am developing an app with SwiftUI using CoreData and iCloudKit to sync data between platforms. The problem is that the iCloud background update is not being triggered when staying in the application. If I make changes on both systems, the changes are being pushed, however not visible on the other device. I need to reload the app, close the app and open again. I already enabled iCloud capability, background notifications and push notifications. This is my persistentContainer var persistentContainer: NSPersistentCloudKitContainer = { let container = NSPersistentCloudKitContainer(name: "Test7") container.loadPersistentStores(completionHandler: {(StoreDescription, error) in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo)") } }) container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy return container }() func saveContext() { let context = persistentContainer.viewContext if context.hasChanges{ do { try context.save() } catch { let nserror = error as NSError fatalError("Unresolved error \(nserror), \(nserror.userInfo)") } } } This is my model class ItemsModel: ObservableObject { init() { readData() } @Published var dataInputs: [Item] = [] let context = persistentContainer.viewContext func readData(){ let request: NSFetchRequest<Item> = Item.fetchRequest() do { let results = try context.fetch(request) self.dataInputs = results } catch { print(error.localizedDescription) } } func addItem(todo: String, date: Date){ let entity = NSEntityDescription.insertNewObject(forEntityName: "Item", into: context) as! Item entity.todo = todo entity.date = date do { try context.save() self.dataInputs.append(entity) } catch { print(error.localizedDescription) } } func deleteItems(indexSet: IndexSet){ for index in indexSet{ do { let obj = dataInputs[index] context.delete(obj) try context.save() let index = dataInputs.firstIndex(of: obj) dataInputs.remove(at: index!) } catch { print(error.localizedDescription) } } } } and this is my view struct ContentView: View { @EnvironmentObject var items: ItemsModel var body: some View { NavigationView{ List { ForEach(items.dataInputs) { item in Text("Item at \(item.date!)") } .onDelete(perform: items.deleteItems) } .toolbar { Button { items.addItem(todo: "Hello", date: Date()) } label: { Image(systemName: "plus") } } } } } Thank you
3
0
2.5k
Jun ’21
PersistenceController and CloudKit
Hello I am making a To-Do list app where I use CoreData and CloudKit, the problem is that when I added this line of code container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: fileContainer.appendingPathComponent("MJ.sqlite"))] to the PersistenceController, iCloud syncing stopped working. (I need that line of code in order to permit to extensions to access the CoreData database) Any idea to solve the problem? This is all the PersistenceController code struct PersistenceController { static let shared = PersistenceController() let container: NSPersistentCloudKitContainer init(inMemory: Bool = false) { container = NSPersistentCloudKitContainer(name: "MJ") guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.name") else { fatalError("Shared file container could not be created.") } container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: fileContainer.appendingPathComponent("MJ.sqlite"))] container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo)") } }) container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy } } Thank you!
3
0
2.5k
Dec ’21
App increases size even if I delete data (Core Data)
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!
3
0
1.6k
Aug ’21
Rotate View around an other View SwiftUI
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!
3
0
2.2k
Sep ’21
.onDelete resets NSPredicate (Core Data, SwiftUI)
Hello I have a list of data in SwiftUI. The data shown in the list can be saved or deleted by using Core Data. In the @FetchRequest property that I am using to display data, I initialized an NSPredicate and in the view, I gave the possibility to the user to change the value of the predicate so that he can filter data, and that is all working, the problem shows up when I delete data from the list when I do so the predicate becomes nil and I don't know why. Here is the code struct SectionList: View { @FetchRequest( entity: LifetimeInputs.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \LifetimeInputs.date, ascending: true)], predicate: nil ) var lifetimeInputsModel: FetchedResults<LifetimeInputs> @FetchRequest(entity: Limit.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Limit.date, ascending: false)]) 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() @Environment(\.managedObjectContext) private var viewContext var conversion: Double { if !limit.isEmpty{ switch limit.last?.unita { case Unit.ml.rawValue: return 1 case Unit.oz.rawValue: return 29.574 default: return 1 } } return 1 } @State private var wantsToFilter: Bool = false @State private var dateSelected = Date() var body: some View { Section{ HStack{ Text("Filter") Spacer() Image(systemName: wantsToFilter ? "checkmark.circle" : "xmark") .font(.system(size: size + 6)) .foregroundColor(wantsToFilter ? .green : .red) .onTapGesture { wantsToFilter.toggle() if wantsToFilter{ lifetimeInputsModel.nsPredicate = NSPredicate( format: "date >= %@ && date <= %@", Calendar.current.dateInterval(of: .day, for: dateSelected)!.start as CVarArg, Calendar.current.dateInterval(of: .day, for: dateSelected)!.end as CVarArg ) } else{ lifetimeInputsModel.nsPredicate = nil } } } DatePicker("Date", selection: $dateSelected, displayedComponents: .date) } header: { Text("Filter") .font(.system(size: size - 4)) } .onChange(of: dateSelected, perform: { _ in if wantsToFilter{ lifetimeInputsModel.nsPredicate = NSPredicate( format: "date >= %@ && date <= %@", Calendar.current.dateInterval(of: .day, for: dateSelected)!.start as CVarArg, Calendar.current.dateInterval(of: .day, for: dateSelected)!.end as CVarArg ) } }) Section{ ForEach(lifetimeInputsModel){ lifetimeInputs in HStack{ Text("\(lifetimeInputs.valori / conversion, specifier: format(unita: !limit.isEmpty ? limit[limit.count - 1].unita ?? ml : ml)) \(!limit.isEmpty ? limit[limit.count - 1].unita ?? ml: ml)") .font(.system(size: size)) Spacer() Text("\(dateFormatter.string(from: lifetimeInputs.date ?? Date()))") .font(.system(size: size)) } } .onDelete{lifeTimeInputsViewModel.deleteItems(offsets: $0, lifetimeInputsModel: lifetimeInputsModel); } } header: { Text("History \(lifetimeInputsModel.count)".localized()).font(.system(size: size - 4)) } } } Thank You!
3
0
1.5k
Jan ’22
Siri Shortcuts with CoreData
Hello I created a simple SwiftUI app with Core Data and I want to be able to add data via the shortcuts app, I created a shortcut that takes some text as input and returns it in uppercase and when I run the shortcut in the shortcuts app, it works, however when I added an "add" function (to save data in the Core Data database) to the intent handle function, and I run it again nothing is saved in the app, here is the code: class MakeUppercaseIntentHandler: NSObject, MakeUppercaseIntentHandling { let persistenceController = PersistenceController() func handle(intent: MakeUppercaseIntent, completion: @escaping (MakeUppercaseIntentResponse) -> Void) { if let inputText = intent.text { let uppercaseText = inputText.uppercased() completion(MakeUppercaseIntentResponse.success(result: add(text: uppercaseText))) } else { completion(MakeUppercaseIntentResponse.failure(error: "The text entred is invalid")) } } func resolveText(for intent: MakeUppercaseIntent, with completion: @escaping (MakeUppercaseTextResolutionResult) -> Void) { if let text = intent.text, !text.isEmpty { completion(MakeUppercaseTextResolutionResult.success(with: text)) } else { completion(MakeUppercaseTextResolutionResult.unsupported(forReason: .noText)) } } func add(text: String) -> String{ let newItem = Item(context: persistenceController.container.viewContext) newItem.text = text do { try persistenceController.container.viewContext.save() } catch { let nsError = error as NSError fatalError("Unresolved error \(nsError), \(nsError.userInfo)") } return text } } Thank You
3
0
1.1k
Dec ’21
Cannot use instance member 'name' within property initializer; property initializers run before 'self' is available
Hello Why does it give me an error when I pass 'name' at line 3? struct OtherView: View { @State private var name: String = "" @ObservedObject var use = Use(name: name) var body: some View{ VStack{ } } } class Use: ObservableObject { @Published var name: String init(name: String) { self.name = name } } Thank you
2
0
4.4k
Apr ’21
Filter in ForEach SwiftUI
Hello I implemented the filter function in my ForEach loop, and it works just with the valori property but not with the date property , is there a way to let it filter also the date? I tried to remove the dateFormatter but it didn't work. Here is the code import SwiftUI let dateFormatter: DateFormatter = { let formatter = DateFormatter() formatter.dateStyle = .medium return formatter }() struct Test4: View { @State private var text: String = "" var body: some View { NavigationView{ if !lifetimes.isEmpty{ List{ Section(header: Text("")){ TextField("Search", text: $text) } Section(header: Text("")){ ForEach(lifetimes.filter { text.isEmpty || "\($0)".contains(text) }, id: \.id){ lifetimeInputs in HStack{ Text("\(lifetimeInputs.valori, specifier: "%.0f")") Spacer() Text("\(dateFormatter.string(from: lifetimeInputs.date))") } } } } .listStyle(InsetGroupedListStyle()) .navigationTitle("All History") } else{ VStack{ Text("No Data") .font(.largeTitle) .fontWeight(.semibold) .foregroundColor(.secondary) } .padding(.bottom) .navigationTitle("All History") } } } } struct LifetimeInputsModel: Identifiable { var id = UUID() var valori: Double var date: Date } var lifetimes: [LifetimeInputsModel] = [ LifetimeInputsModel(valori: 300, date: Date()), LifetimeInputsModel(valori: 200, date: Date() + 86400) ] Thank you
2
0
2.9k
Jun ’21
HealthKit (delete function in SwiftUI)
Hello I am trying to save some data in the Health App from my app, and it is working, the problem is that when I delete that data (already saved) from my app (using the deleteFromHealthKit function) the data is not deleted from the health app. How can I fix this? Here is the code: import SwiftUI import HealthKit struct ContentView: View { init() { //-------- let healthStore = HKHealthStore() let allTypes = Set([HKObjectType.quantityType(forIdentifier: .dietaryWater)!]) healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in if !success { print("success") } } } func fetchHealthData(date: Date, ml: Double) -> Void { let healthStore = HKHealthStore() let quantityType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.dietaryWater) let waterConsumed = HKQuantitySample.init(type: quantityType!, quantity: .init(unit: HKUnit.literUnit(with: .milli), doubleValue: ml), start: date, end: date) healthStore.save(waterConsumed) { success, error in if (error != nil) { print("Error: \(String(describing: error))") } if success { print("Saved: \(success)") } } } @State var water: [Water] = [] @State private var value: Double = 0 func deleteFromHealthKit(date: Date, ml: Double) { let healthStore = HKHealthStore() let quantityType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.dietaryWater) let waterConsumed = HKQuantitySample.init(type: quantityType!, quantity: .init(unit: HKUnit.literUnit(with: .milli), doubleValue: ml), start: date, end: date) healthStore.delete(waterConsumed) { success, error in if (error != nil) { print("Error: \(String(describing: error))") } if success { print("Saved: \(success)") } } } var body: some View { NavigationView{ VStack{ Text("Value: \(value)") .padding() HStack{ Text("100 ml") .onTapGesture { value = 100 } Text("200 ml") .onTapGesture { value = 200 } } Button("Add"){ water.append(Water(value: value, date: Date())) fetchHealthData(date: Date(), ml: value) }.disabled(value == 0 ? true : false) .padding() List{ ForEach(0..<water.count, id: \.self){ i in HStack{ Text("\(water[i].value)") Text("\(water[i].date)") } .onTapGesture { deleteFromHealthKit(date: water[i].date, ml: water[i].value) water.remove(at: i) } } } } } } } struct Water: Identifiable { var id = UUID() var value: Double var date: Date } Thank you
2
0
1.6k
Jun ’21
SwiftUI & Layout API. Extra trailing closure passed in call
Hello I'm trying to compose a layout using the Layout API. I have already written the code for both the Layout Stack I want to use and the view I am using it in, however I am getting an "Extra trailing closure passed in call" error in the view I am using the Stack in. Here is the code: import SwiftUI struct StairsView: View { var body: some View { Group{ MyStairsStack{ Text("Hello, World!") Text("Hello, World!") Text("Hello, World!") } } } } struct MyStairsStack: Layout{ func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout Void) -> CGSize { return .init(width: proposal.width ?? 0, height: proposal.height ?? 0) } func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout Void) { guard !subviews.isEmpty else { return } let viewSize = maxSize(subViews: subviews) var origin = bounds.origin let maxWidth = bounds.width subviews.forEach { view in if (origin.x + (viewSize.width + 10) >= maxWidth){ origin.x = bounds.origin.x } view.place(at: origin, proposal: proposal) origin.x += (viewSize.width + 10) origin.y += (viewSize.height + 10) } } private func maxSize(subViews: Subviews) -> CGSize{ subViews.map { $0.sizeThatFits(.unspecified) }.reduce(.zero) { currentMax, subviewSize in CGSize( width: max(currentMax.width, subviewSize.width), height: max(currentMax.height, subviewSize.height)) } } } The error is at line 5 Thank You for your time
2
0
1.8k
Jun ’22