core-data

Core Data - Decimal Type for Currency

半世苍凉 提交于 2020-08-26 16:28:32
问题 I read in a recent post that the Decimal data type is the best to use in Core Data to model currency since it doesn't have floating point issues. I've searched the apple docs and google and can't seem to find any documentation on using the Decimal type. Does anyone have a link to documentation on it? Thanks! 回答1: Use "Decimal" as the attribute type in your data model and NSDecimalNumber as the object type in your code. 回答2: The 'decimal' type in Core Data relates to the Cocoa type

save image to binary data of a ordered index

风流意气都作罢 提交于 2020-08-10 23:02:11
问题 My swift code below is trying to to append the image that is in the pic image and save it into core data. The core data attributes are located in a image below. When the user enters 3 into the textfield the image the saved into core data should appear because that is the order in the index. 2 Images are saved when build so the new image should start at 3 on the index. import UIKit import CoreData class ViewController: UIViewController,UITextFieldDelegate, UIImagePickerControllerDelegate,

Crash occurs when executing Share Extension twice in system Files to share file to app

与世无争的帅哥 提交于 2020-08-07 05:07:33
问题 The app uses Share Extension to import a string of .txt files to Core Data. And then syncs the Core Data to iCloud. There is an entity called Item . When sharing a new item in system Files through Share Extension, the code needs to calculate the order for the new item to be imported. And the code is: import Foundation import CoreData @objc(Item) public class Item: NSManagedObject, Identifiable { class func nextOrder() -> Int { let keyPathExpression = NSExpression.init(forKeyPath: "order") let

How to use @Fetchrequest outside a View

孤者浪人 提交于 2020-08-07 03:51:59
问题 I am trying to move my @fetchrequest property to an auxiliar class, which is not a View , but every time I try to do that, I get a bad instruction error. Can anyone help me? This is a sample of my code: ViewModel: class ViewModel { @FetchRequest(entity: Teste.entity(), sortDescriptors: []) var teste: FetchedResults<Teste> } View: struct ContentView: View { let viewModel: ViewModel init(viewModel: ViewModel) { self.viewModel = viewModel } var body: some View { List(viewModel.teste) { item in /

Core Data not saving objects persistently

陌路散爱 提交于 2020-08-03 12:21:42
问题 I'm new to Core Data and as such am not sure if I'm making a mistake. I've downloaded some data from a REST API and it successfully saves the JSON response to disk. I'm trying to process the data and save it persistently using Core Data. NSLog(@"inserted objects: %@", [managedObjectContext insertedObjects]); [managedObjectContext performBlockAndWait:^{ NSError *error = nil; if (![managedObjectContext save:&error]) { NSLog(@"Unable to save context for class %@", className); } else { NSLog(@

Using @fetchRequest(entity: ) for SwiftUI macOS app crashes

天涯浪子 提交于 2020-08-02 08:12:10
问题 I'm trying to run a basic test SwiftUI app for macOS using Core Data, and I'm hitting a problem. When I use this in my view: @FetchRequest(entity: Note.entity(), sortDescriptors: []) let notes: FetchedResults<Note> The app crashes with the following errors: NoteTaker [error] error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'NoteTaker.Note' so +entity is confused. Have you loaded your NSManagedObjectModel yet ? CoreData: error: No NSEntityDescriptions in any model

SwiftUI DatePicker Binding optional Date, valid nil

浪尽此生 提交于 2020-07-26 18:18:35
问题 I'm experimenting code from https://alanquatermain.me/programming/swiftui/2019-11-15-CoreData-and-bindings/ my goal is to have DatePicker bind to Binding< Date? > which allow for nil value instead of initiate to Date(); this is useful, if you have Date attribute in your core data model entity which accept nil as valid value. Here is my swift playground code: extension Binding { init<T>(isNotNil source: Binding<T?>, defaultValue: T) where Value == Bool { self.init(get: { source.wrappedValue !=

SwiftUI DatePicker Binding optional Date, valid nil

主宰稳场 提交于 2020-07-26 18:11:19
问题 I'm experimenting code from https://alanquatermain.me/programming/swiftui/2019-11-15-CoreData-and-bindings/ my goal is to have DatePicker bind to Binding< Date? > which allow for nil value instead of initiate to Date(); this is useful, if you have Date attribute in your core data model entity which accept nil as valid value. Here is my swift playground code: extension Binding { init<T>(isNotNil source: Binding<T?>, defaultValue: T) where Value == Bool { self.init(get: { source.wrappedValue !=

SwiftUI DatePicker Binding optional Date, valid nil

徘徊边缘 提交于 2020-07-26 18:10:49
问题 I'm experimenting code from https://alanquatermain.me/programming/swiftui/2019-11-15-CoreData-and-bindings/ my goal is to have DatePicker bind to Binding< Date? > which allow for nil value instead of initiate to Date(); this is useful, if you have Date attribute in your core data model entity which accept nil as valid value. Here is my swift playground code: extension Binding { init<T>(isNotNil source: Binding<T?>, defaultValue: T) where Value == Bool { self.init(get: { source.wrappedValue !=

delete Single Core Data Item from a uicollectionviewCell using a button

蓝咒 提交于 2020-07-23 07:40:11
问题 I am trying to delete a single element of the coreData set using a button inside a collectionviewcell. So the cell has a label with the result of the core data fetch. When I hit the button it should delete the cell making it no longer appear. Also the the core data single set item should be perementley deleted. I have scence this used before in protocols but not using core data.The core data is var itemName. class CustomCell: UICollectionViewCell { @objc func delete() { }} class