Post

Replies

Boosts

Views

Created

Filtering an Array
I'm trying to filter an array containing Recipes that have a property called title of type String that is required. These are the variables used in the line of code: @State private var searchingFor = "" @State private var filteredRecipes = [Recipe]() This is the line of code giving me the error: filteredRecipes=filteredRecipes.filter{$0.title.contains(searchingFor)} The error: "Value of optional type 'String?' must be unwrapped to refer to member 'contains' of wrapped base type 'String'" Any ideas on how I need to rewrite that line of code so that I don't get that error? ie. how do I unwrap 'String?'
3
0
1.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
562
Jun ’22
Airdrop CoreData
I need to share a singular instance of an object created in core data. Every video I've found that showed how to do this used outdated code and I was wondering if anyone had a working way to either create the airdrop button (because I don't even know how to do that yet, or make it so that the data is share is a duplicate of the object from the sharers device. Thanks! This is the entity that needs to be shared via airdrop:
0
0
309
Jun ’22
List Background
Does anyone have any clue as to how to get rid of the grey background of my list? I've actually tried everything I could find online. .background(Color.clear), .backgroundStyle(.clear), init(){...}. nothing. I even tried replicating it in a new project and it didn't even show up there. Any idea as to how to get rid of this?
1
0
483
Jun ’22
NavBar Format
How has GitHub formatted their navigation bar like this? The title is aligned with the back button and add button, the search bar is inside instead of under, and they have buttons alone the bottom that filter the list. Is there a view object that shortcuts this? If you understand even one part of that I would appreciate that information. Thanks!
0
0
342
Jun ’22
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
405
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