Post

Replies

Boosts

Views

Activity

Enlarged TextField
I need to enlarge my text field. So I tried using a frame, but that only put dead space around it. The problem with that is I want to be able to click anywhere inside the boarder and it pull up the keyboard. So I tried padding and that did the same thing. Also I need the text to start a new line after it fills the first one and to go for as long as they type. Thanks!
1
0
406
Jun ’22
View References
I need to edit an attribute of a core data entity fetched in a previous view. So I put var entityName: FetchedResults.Elementin the second view to get access to the entity. But when I do this it doesn't actually get the entity it gets a copy of the entity so when changes are made to it it doesn't actually change. Basically is there a way to pass a reference to an attribute of an object to a struct so that when you set that reference to a value the entity changes? Any help would be greatly appreciated. All the best!
1
0
571
Jun ’22
Sheet Won't Display in a ContextMenu
So this project is pretty straightforward. I have an item. The use can create, get a closer look at, delete, or edit an item. For some reason, no matter how many different ways I try to do it, the edit part does not work. I've tried moving the sheet outside of the context menu (it only opens the bottom item), I've tried moving it under the list using the first index of the item (it crashes), and finally I've tried it the shown way and it just doesn't do anything. If anyone can come up with a better way to open the edit view for the correct item (preferably using a context menu, but something similar is acceptable) I would really appreciate it. I've provided the project for a better understanding of what I'm trying to do. If you have any questions just leave a comment. Any help would be greatly appreciated. Content View: struct ContentView: View {     @Environment(\.managedObjectContext) var managedObjContext     @ObservedObject var persistence = PersistenceController.shared     @State private var items = PersistenceController.shared.getItems()     @State private var showingEditView = false     @State private var showingAddView = false          var body: some View {         NavigationView{             List{                 Section(""){                     ForEach(items) { item in                         NavigationLink(destination: ItemView(item: item)){                             Text(item.name!)                         }                         .contextMenu{                             Button(action: {                                 self.showingEditView.toggle()                             }){                                 Text("Edit Item")                             }                             .sheet(isPresented: $showingEditView){                                 EditItemView(item: item)                                     .onDisappear(perform: {                                         items = persistence.getItems()                                     })                             }                         }                     }                     .onDelete(perform: { indexSet in                         deleteItem(indexSet: indexSet)                     })                 }             }             .listStyle(InsetGroupedListStyle())             .cornerRadius(10)             .navigationBarTitle("My Items")             .navigationBarItems(trailing: addButton)             .onAppear(perform: {                 items = persistence.getItems()             })             .sheet(isPresented: $showingAddView){                 AddItemView()                     .onDisappear(perform: {                         items = persistence.getItems()                     })             }         }     }          var addButton: some View {         Button(action: {             showingAddView.toggle()         }){             Text("Add an Item").bold()         }     }          func deleteItem(indexSet: IndexSet){         withAnimation{             indexSet.map {                 items[$0] }                         .forEach(managedObjContext.delete)                          persistence.contextSave()             items = persistence.getItems()         }     } } Item View: struct ItemView: View{     @State var item: Item     var body: some View{         Text(item.name ?? "No Name")     } } Add View: struct AddItemView: View{     @Environment(\.dismiss) var dismiss     @ObservedObject var persistence = PersistenceController.shared     @State private var name = ""     var body: some View {         Form{             TextField("Item Name", text: $name)             Button(action:{                 persistence.addItem(name: name)                 dismiss()             }){                 Text("Add Item")             }         }     } } Edit View: struct EditItemView: View{     @Environment(\.dismiss) var dismiss     @ObservedObject var persistence = PersistenceController.shared     @State var item: Item     @State private var name = ""     var body: some View {         Form{             TextField("Item Name", text: $name)             Button(action:{                 persistence.addItem(name: name)                 dismiss()             }){                 Text("Add Item")             }         }         .onAppear{             name=item.name ?? "No Name"         }     } } Persistence File: class PersistenceController : ObservableObject{     static let shared = PersistenceController()     let container: NSPersistentContainer          init(inMemory: Bool = false) {         container = NSPersistentContainer(name: "Test")         if inMemory {             container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")         }         container.loadPersistentStores(completionHandler: { (storeDescription, error) in             if let error = error as NSError? {                 fatalError("Unresolved error \(error), \(error.userInfo)")             }         })         container.viewContext.automaticallyMergesChangesFromParent = true     }     static var preview: PersistenceController = {         let result = PersistenceController(inMemory: true)         let viewContext = result.container.viewContext         for _ in 0..<10 {             let newItem = Item(context: viewContext)             newItem.id = UUID()             newItem.name = "Test"         }         do {             try viewContext.save()         } catch {             let nsError = error as NSError             fatalError("Unresolved error \(nsError), \(nsError.userInfo)")         }         return result     }()     func getItems() -> [Item] {         let context = container.viewContext         var request = NSFetchRequest<Item>()         request = Item.fetchRequest()         request.entity = NSEntityDescription.entity(forEntityName: "Item", in: context)         do {             let items = try context.fetch(request)             if items.count == 0 { return []}             return items.sorted(by: {$0.name! > $1.name!})         } catch {             print("**** ERROR: items fetch failed \(error)")             return []         }     }          func addItem(name: String){         let context = container.viewContext         let item = Item(context: context)         item.id = UUID()         item.name = name                  contextSave()     }          func contextSave() {         let context = container.viewContext         if context.hasChanges {             do {                 try context.save()                 self.objectWillChange.send()             } catch {                 print("**** ERROR: Unable to save context \(error)")             }         }     } } Data Model:
1
0
1k
Jun ’22
Broken Date List
I need help optimizing and fixing my date list builder. Basically the user should be able to build a list of dates in a sheet and it be shown on the original view and go back to that page to add more. Dates should not be repeated hence why I use a set. Issue: But when the user adds the first date it doesn't show until a second date is added. After some troubleshooting with print I've found that the problem isn't adding the first item to the list, but the ForEach not working until there is a second item. I've included code for a visual representation of my goal. If you have any questions please leave a comment. Any help would be greatly appreciated. Thanks! import SwiftUI struct ContentView: View {     @State private var dates = Set<Date>()     @State private var isDatesShowing = false     var body: some View {         NavigationView{                 Form{                     Section(""){                         addDatesButton                         Section{                             ForEach(dates.sorted(by: <),id: \.self){ date in                                 Text(date, style: .date)                                     .bold()                                     .foregroundColor(.white)                             }                         }                     }                 }                 .navigationBarTitle("List of Dates")                 .sheet(isPresented: $isDatesShowing){                     AddToDateView(dates: $dates)                 }         }         .preferredColorScheme(.dark)     }     var addDatesButton: some View{         Button(action: {             isDatesShowing.toggle()         }){             Text("Select Dates")                 .bold()         }     } } struct AddToDateView: View {     @Environment(\.dismiss) var dismiss     @Binding var dates: Set<Date>     @State var date = Date()          var body: some View {         NavigationView{             Form{                 DatePicker(selection: $date, in: Date.now..., displayedComponents: .date) {                     Text("Select a date")                 }                 .datePickerStyle(WheelDatePickerStyle())                 Button(action:{                     dates.insert(date)                 }){                     Text("Add Date")                 }                 Section("Dates"){                     ForEach(dates.sorted(by: <),id: \.self){ date in                         Button(action: {                             dates.remove(date)                         }){                             Text(date, style: .date)                                 .bold()                                 .foregroundColor(.white)                         }                     }                 }             }             .cornerRadius(10)             .navigationBarItems(trailing: submitButton)                  }     }          var submitButton: some View{         Button(action: {             dismiss()         }){             Text("Submit")                 .bold()         }     } }
1
0
704
Jun ’22
Airdropping Core Data
So I know this is probably a stretch, but is there any way to airdrop an object from core data? From what I understand airdrop is only used with links, but is there a way to convert the data of a core data object into a file, convert the file into a link, send it via airdrop, and convert it back into a file, and then add that object from a file to another user's core data storage? Any help would be greatly appreciated. Thanks!!
1
0
1.3k
Jul ’22
Resetting a View With a Tab Item
So I have a TabView in my app and I want to go back to the original view when that tabItem is pressed. For example, when you open the App Store and go to the games tab, click on a game. It takes you to a navigation link to a new view. You can either go back to the first page by pressing the arrow, or by tapping the “games” tabItem again. I want to be able to press my tabItem in a subview of the same tab to go back to the original view that the tab is assigned to. Any help would be greatly appreciated! Thanks.
1
0
833
Jul ’22
Data Persistence using Core Data
So I created a program without selecting “use core data” and realized after trying to make a persistence data storage that it helps very much so I created a new program and selected it this time and copied everything over. It provided a file called “Persistence” and the contentView file had a bunch of stuff already filled in (Also something called the title of the program). I have the data I need saved to the persistent data storage narrowed down to a singular array, but none of the videos I found online showed this version of xcode that supplied a “Persistence” file when using core data so I’m unsure how to use it. I will provide the contentView and Persistence file for context. The array I need saved is called mainList in contentView. ContentView: import SwiftUI import CoreData struct ContentView: View {     var mainList = [RecipeList(),RecipeList(),RecipeList(),RecipeList(),RecipeList()]          @Environment(\.managedObjectContext) private var viewContext     @FetchRequest(         sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],         animation: .default)     private var items: FetchedResults<Item>     var body: some View {         NavigationView {             List {                 ForEach(items) { item in                     NavigationLink {                         Text("Item at \(item.timestamp!, formatter: itemFormatter)")                     } label: {                         Text(item.timestamp!, formatter: itemFormatter)                     }                 }                 .onDelete(perform: deleteItems)             }             .toolbar {                 ToolbarItem(placement: .navigationBarTrailing) {                     EditButton()                 }                 ToolbarItem {                     Button(action: addItem) {                         Label("Add Item", systemImage: "plus")                     }                 }             }             Text("Select an item")         }     }     private func addItem() {         withAnimation {             let newItem = Item(context: viewContext)             newItem.timestamp = Date()             do {                 try viewContext.save()             } catch {                 // Replace this implementation with code to handle the error appropriately.                 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.                 let nsError = error as NSError                 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")             }         }     }     private func deleteItems(offsets: IndexSet) {         withAnimation {             offsets.map { items[$0] }.forEach(viewContext.delete)             do {                 try viewContext.save()             } catch {                 // Replace this implementation with code to handle the error appropriately.                 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.                 let nsError = error as NSError                 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")             }         }     } } private let itemFormatter: DateFormatter = {     let formatter = DateFormatter()     formatter.dateStyle = .short     formatter.timeStyle = .medium     return formatter }() struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)     } } Persistence: import CoreData struct PersistenceController {     static let shared = PersistenceController()     static var preview: PersistenceController = {         let result = PersistenceController(inMemory: true)         let viewContext = result.container.viewContext         for _ in 0..<10 {             let newItem = Item(context: viewContext)             newItem.timestamp = Date()         }         do {             try viewContext.save()         } catch {             // Replace this implementation with code to handle the error appropriately.             // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.             let nsError = error as NSError             fatalError("Unresolved error \(nsError), \(nsError.userInfo)")         }         return result     }()     let container: NSPersistentCloudKitContainer     init(inMemory: Bool = false) {         container = NSPersistentCloudKitContainer(name: "ReciStorage")         if inMemory {             container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")         }         container.loadPersistentStores(completionHandler: { (storeDescription, error) in             if let error = error as NSError? {                 // Replace this implementation with code to handle the error appropriately.                 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.                 /*                  Typical reasons for an error here include:                  * The parent directory does not exist, cannot be created, or disallows writing.                  * The persistent store is not accessible, due to permissions or data protection when the device is locked.                  * The device is out of space.                  * The store could not be migrated to the current model version.                  Check the error message to determine what the actual problem was.                  */                 fatalError("Unresolved error \(error), \(error.userInfo)")             }         })         container.viewContext.automaticallyMergesChangesFromParent = true     } } Image showing the thing named the title of the program that I’m certain is relevant to the persisting data storage: Also I’m unsure what I need to replace those comments with and what subclasses I should add to existing swift files like “codable” for example. Any help would be greatly appreciated.
2
1
1.8k
Jun ’22
Core Data Entity Inside Another Entity
So I have an entity in my core data model called recipe. I need to create another entity containing a recipe and a date that the recipe is assigned to. Can I do this similar to the way I've done it in the image and just save a Recipe object in the initialization of PlannedRecipe object in the Persistence file? Basically I just need to know how to add a entity in an entity using this core data model and persistence file. Persistence file: import CoreData struct PersistenceController {     static let shared = PersistenceController()     let container: NSPersistentCloudKitContainer     init(inMemory: Bool = false) {         container = NSPersistentCloudKitContainer(name: "ReciPrep")         if inMemory {             container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")         }         container.loadPersistentStores(completionHandler: { (storeDescription, error) in             if let error = error as NSError? {                 fatalError("Unresolved error \(error), \(error.userInfo)")             }         })         container.viewContext.automaticallyMergesChangesFromParent = true     }     func addPlannedRecipe(recipe: Recipe,date: Date, context: NSManagedObjectContext){         let plannedRecipe = PlannedRecipe(context: context)         plannedRecipe.id = UUID()         plannedRecipe.date = Date()         plannedRecipe.recipe = recipe //Giving me an error: "Cannot assign value of type 'Recipe' to type 'Data?'"                  save(context: context)     }          func save(context: NSManagedObjectContext){         do {             try context.save()         } catch {             let nsError = error as NSError             fatalError("Unresolved error \(nsError), \(nsError.userInfo)")         }     } } I guess this problem can be solved if I can convert a recipe into binary data or some other savable data in an entity. Any help would be greatly appreciated.
2
0
1.1k
Jun ’22
FetchedResults to Array (Error)
So I've found a way to convert fetched results to an array of the same data type, and not only that but filter them with the fetch request given a string: func searchResults(searchingFor: String)->[Recipe]{     var filteredRecipeList=[Recipe]()     @FetchRequest(sortDescriptors: [SortDescriptor(\.date, order: .reverse)], predicate: NSPredicate(format: "title CONTAINS[c] %@",searchingFor)) var filteredResults: FetchedResults<Recipe>     for recipe in filteredResults {         filteredRecipeList.append(recipe)     }     return filteredRecipeList } To clarify, this would ideally return an array with a list of Recipes that contain the given string in the title. In theory this should work just fine, but I'm getting a weird error. I've never seen an error like this and I'm not sure how to understand it. It is purple with a yellow warning. The error says "Accessing StateObject's object without being installed on a View. This will create a new instance each time." How do I get around this issue to accomplish what I'm trying to accomplish. Thanks in advance for any help whatsoever. I'll upvote anyone with any bit of helpful information. Have a good one!
2
1
2.3k
Jun ’22
Changing Views Bar
I’ve been looking for how to do this for AGES and I’ve finally found and app made in Swift that has it. So I really need a bar that stays at the bottom of my screen even when scrolling or changing views that has buttons on it that takes the user to a different view. And I don’t mean like a NavigationLink with a back button and a sliding transition. I mean I want it to open a whole new view and save the user’s location in the other view so when they press the button to return to the previous view they continue where they left off. This is the GitHub app and they’ve executed exactly what I need: To be clear this is what I’m talking about: If anyone has a repository or a YouTube tutorial that has something like this I would be SO grateful. Thanks! :)
2
0
564
Jun ’22
Time Wheel Picker
I need a wheel picker for hours and minutes. I've found stuff online but it does have the "Hours" and "Minutes" right after and it isn't as compact. Is there a newer way to accomplish this to look exactly like it does in the timer app?
2
0
1.8k
Jun ’22
Unknown Errors From Inverse Relationship. Please Help!!!
I had the idea of adding a list feature in my app where users can create a list and add multiple recipes to the app so first and only thing I did was add an entity to my data model called "List" that contains a string: title and an array of Recipes: recipes (I remembered to put "NSSecureUnarchiveFromData" in Transformer and put [Recipe] for custom class). Afterwards I made the relationship in both entities and made them inverse. I made no other changes to my code. But I ran it just to make sure nothing went wrong and lo and behold: 7 never before seen errors, but only in one file. Before adding this entity this same code compiled just fine. This is the file and these are the errors I'm getting. Any help would be greatly appreciated. import SwiftUI struct RecipeView: View {     @Environment (\.managedObjectContext) var managedObjContext     @Environment(\.dismiss) var dismiss          var recipe: FetchedResults<Recipe>.Element     @State var isFavorite: Bool     @State var servings = -1          var body: some View {         VStack(alignment: .leading){ //Error: Trailing closure passed to parameter of type 'CGFloat?' that does not accept a closure             if (recipe.notes! != ""){                 Section{                     Text(recipe.notes!)                         .font(.headline)                 }                 .padding(.horizontal)             }             HStack{                 Spacer()                 Text("Total Time: "+calcTime(time:Int(recipe.totalTime!) ?? 0))                 Spacer()                 Text("Servings: "+recipe.servings!)                 Spacer()             }             .padding(.vertical)             Grid{                 GridRow{                     Button {                         isFavorite.toggle()                         recipe.isFavorite.toggle()                         PersistenceController().save(context: managedObjContext)                     } label: {                         HStack{                             Image(systemName: isFavorite ? "star.fill" : "star")                                 .foregroundStyle(.yellow)                             Text(isFavorite ? "Unfavorite" : "Favorite")                                 .foregroundColor(Color(UIColor.lightGray))                         }                         .frame(width: 300,height: 50)                         .background(Color(UIColor(hexString: "#202020")))                         .border(Color(UIColor(hexString: "#202020")))                         .cornerRadius(5)                     }                     Button {                         print("implement list functionality")                     } label: {                         Image(systemName: "plus")                             .frame(width: 50,height: 50)                             .background(Color(UIColor(hexString: "#202020")))                             .border(Color(UIColor(hexString: "#202020")))                             .cornerRadius(5)                     }                 }             }             .padding(.horizontal)             List{                 NavigationLink(destination: ingredientsView(ingredients: recipe.ingredients!)){                     HStack{                         Text("List of Ingredients")                         Spacer()                         Text(String(recipe.ingredients!.count))                             .foregroundColor(.gray)                     }                 }                 .frame(height: 50)                 NavigationLink(destination: instructionsView(instructions: recipe.instructions!)){                     HStack{                         Text("List of Instructions")                         Spacer()                         Text(String(recipe.instructions!.count))                             .foregroundColor(.gray)                     }                 }                 .frame(height: 50)             }             .listStyle(.grouped)             .scrollDisabled(true)             Spacer()         }         .navigationBarTitle(recipe.title!)         .navigationBarItems(trailing: shareButton)         .onAppear{             PersistenceController().updateDate(recipe: recipe, context: managedObjContext)         }         Spacer()     }     var shareButton: some View{         Button(action: {             print("Implement airdrop feature")         }){             Image(systemName: "square.and.arrow.up")                 .foregroundStyle(.blue)         }     } } struct ingredientsView: View{     @State var ingredients: [String]     var body: some View{         List{ // Error: Trailing closure passed to parameter of type 'NSManagedObjectContext' that does not accept a closure             Section(""){                 ForEach(ingredients,id: \.self){ String in                     NavigationLink(destination:                                     NavigationView{                         Text(String)                             .frame(alignment:.center)                             .font(.title)                     }){                         Text(String).lineLimit(1)                     }                 }             }         }         .frame(alignment: .center) //Error: Cannot infer contextual base in reference to member 'center' //Error: Value of type 'List' has no member 'frame'         .cornerRadius(10)         .navigationTitle("Ingredients List")     } } struct instructionsView: View{     @State var instructions: [String]     var body: some View{         List{ // Error: Trailing closure passed to parameter of type 'NSManagedObjectContext' that does not accept a closure             Section(""){                 ForEach(instructions,id: \.self){ String in                     NavigationLink(destination:                                     NavigationView{                         Text(String)                             .frame(alignment:.center)                             .font(.title)                     }){                         Text(String).lineLimit(1)                     }                 }             }         }         .frame(alignment: .center) //Error: Cannot infer contextual base in reference to member 'center' //Error: Value of type 'List' has no member 'frame'         .cornerRadius(10)         .navigationTitle("Instructions List")     } }
2
1
1.5k
Jun ’22
Optional Type Date
I'm sorry I know this is basic swift, but how do I get I make the selection an optional return? I know it's with ?? "" with a string, but I don't know how to do it with a date type. Also how do I get a navigationBarTitle to work on this view? The "My Calendar" doesn't show up when I run the code. import SwiftUI struct ReciPrep: View {     @State private var date: Date? = ni     @Environment(\.calendar) var calendar          @State private var showingAddView = false     var body: some View {         VStack{             DatePicker("Calendar", selection: $date, in: Date.now...,displayedComponents: [.date]) //Error: Cannot convert value of type 'Binding<Date?>' to expected argument type 'Binding<Date>'                 .datePickerStyle(.graphical)             Spacer()         }         .navigationBarTitle("My Calendar")     } } Any help would be greatly appreciated. Have a good day!
2
0
2.5k
Jun ’22
EditView Reference is Broken
I've made a simplified version of what I'm trying to do in the included code, but basically I'm trying to make a view that edits a data model. This edit view shows up when a button in a context menu is clicked. The problem is no matter which one I open up the context menu on it always opens up whichever one I clicked first. The reference to the object in the .sheet never changes. How can I fix this?? Example Code: import SwiftUI class Object: Identifiable{     var id: UUID?     var title: String     var string: String        init(title: String, string: String) {         self.id = UUID()         self.title = title         self.string = string     } } struct ContentView: View {     @State private var showingEditView = false     @State private var objectList = [Object(title: "First", string: "Editor"), Object(title: "Second", string: "Addition"), Object(title: "Third", string: "Twelve")]     var body: some View {         NavigationView{             List{                 Section("Objects"){                     ForEach(objectList) { object in                         NavigationLink(destination: ObjectView(obj: object)){                             Text(object.title)                         }                         .sheet(isPresented: $showingEditView){                             EditView(obj: object)                         }                     }                 }                 .contextMenu{                     Button(action: {                         self.showingEditView.toggle()                     }){                         Text("Edit Item")                     }                 }             }             .listStyle(InsetGroupedListStyle())             .cornerRadius(10)             .navigationBarTitle("My List")         }     } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView()     } } struct ObjectView: View {     @State var obj: Object     var body: some View {         NavigationView{             VStack{                 Text(obj.string)             }             .navigationBarTitle(obj.title)         }     } } struct EditView: View {     @Environment(\.dismiss) var dismiss     @State var obj: Object     @State var word = ""     var body: some View {         NavigationView{             Form{                 TextField("Change Word", text: $word)                     .onAppear(perform: {                         word = obj.string                     })             }             .navigationBarItems(trailing:             Button(action: {                     dismiss()             }){                 Text("Done")                     .bold()             })         }     } } Any help would be greatly appreciated. All the best!
2
0
423
Jun ’22