So I was just following along with CodeWithChris's video on make a persistent data storage (https://youtu.be/O7u9nYWjvKk) and I got to the part where he says to include the subclass NSManagedObject to the family and person classes. I have a very similar program with recipe and list of recipes, but when I add "NSManagedObject" I get an error:
< unknown >:0: error: stored property '_title' requires an initial value
CoreData.NSManagedObject:2:12: note: superclass 'NSManagedObject' requires all stored properties to have initial values
open class NSManagedObject : NSObject {
< unknown > :0: error: stored property '_calories' requires an initial value
CoreData.NSManagedObject:2:12: note: superclass 'NSManagedObject' requires all stored properties to have initial values
open class NSManagedObject : NSObject {
etc.
Unsure of how to fix it. Any help would be greatly appreciated.
Code in case I'm missing something
RecipeList:
import Foundation
import CoreData
class RecipeList:NSManagedObject, Identifiable{
@Published var recipeList:[Recipe]
init(){
recipeList=[]
}
func addRecipe(recipe: Recipe){
self.recipeList.append(recipe)
}
func removeRecipe(loc: Int){
self.recipeList.remove(at: loc)
}
func getList() -> Array<Recipe>{
return recipeList
}
}
Recipe:
import Foundation
import UIKit
import CoreData
class Recipe:NSManagedObject, Identifiable{
@Published var title: String
@Published var calories: String
@Published var ingredients: Array<String>
@Published var instructions: Array<String>
@Published var totalTime: String
@Published var notes: String
init(title: String, calories: String, ingredients: Array<String>, instructions: Array<String>, totalTime: String, notes: String){
self.title=title
self.calories=calories
self.ingredients=ingredients
self.instructions=instructions
self.totalTime=totalTime
self.notes=notes
}
}
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I would like to implement the “Toolbar” kind of thing above the keyboard shown in the image that includes an up and down arrow to iterate through text fields and a done button to dismiss the keyboard. How do I do this? Thanks!
So I have a sheet View for when I add a recipe to my program, but when I add an instruction I want the user to be able to click their entry and check it to see if it is correct so what I’ve done is add a text view to enlarge it, but when I go to the view it shows the nav bar of the previous view like this:
This only happens when I use a sheet.
So I’ve looked this up so many times and tried every way I’ve found using a @State variable and nothing works. I need a button that toggles between SystemImage: “Star” to SystemImage: “Star.fill” when clicked. And I want it to happen when it’s clicked. Everything I’ve tried only changes when I close and reopen the view because it happens on appearance. Is there any way to make it change instantly? Thanks for any help!
I would like to implement the “Toolbar” kind of thing above the keyboard shown in the image that includes an up and down arrow to iterate through text fields and a done button to dismiss the keyboard. How do I do this? Thanks!
So I've made a function using FetchRequest to search, but im getting an error: "Accessing StateObject's object without being installed on a View. This will create a new instance each time." The function takes a string and searches the core data for objects containing that string. The string is obtained from a .searchable object and the function is ran as a ForEach parameter.
This is the function:
func searchResults(searchingFor: String) -> FetchedResults<Recipe> {
@FetchRequest(sortDescriptors: [SortDescriptor(\.date, order: .reverse)], predicate: NSPredicate(format: "title CONTAINS[c] %@",searchingFor)) var searchRecipes: FetchedResults<Recipe>
return searchRecipes
}
Is there a better way to do this or can I do it this way and tweak it a little bit? I really just need to have a search bar that returns a FetechedResults type so I can use it in a ForEach loop. Any help is greatly appreciated.
I am trying to implement a search bar in my recipe storage app. I got recommended using the .filter function that I didn’t know could be used on FetchedResults. So I did and now I’m getting errors, but not ones saying that it isn’t in the scope so I believe this is possible, just now sure how.
The ForEach loop containing the filter:
•ForEach(recipes.filter({$0.title.contains(searchingFor)})) { recipe in
Note: Recipes is just a FetchedResults type and searchingFor is just a string that comes from the search bar.
The errors:
•Value of optional type 'String?' must be unwrapped to refer to member 'contains' of wrapped base type 'String'
•Chain the optional using '?' to access member 'contains' only for non-'nil' base values
•Force-unwrap using '!' to abort execution if the optional value contains 'nil'
Any help is greatly appreciated.
Whenever I open a text field almost anywhere I get this common toolbar above my keyboard with two arrows that iterate through text fields and a done button. Not just on Apple related things, but even when I search on google. Which leads me to believe this is a default object and that google did not take the time to make an identical keyboard toolbar as all of Apple’s. So how do I get this default toolbar?
toolbar:
Anyone have a really good YouTube tutorial on how to implement an image as binary data in core data? Thanks!
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:
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!
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?
Anyone have any idea as to how to embed an SwiftUI View in a UITabBarController. From what I understand they were made to contain views made from UIKit and my views are made from SwiftUI so I’m not exactly sure how to use a UITabBarController in the storyboard with the views that I have.
Any help would be greatly appreciated!
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!
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!