My swift code below is trying to save a image litteral to binary core data pic. A image litteral is a image located locally within the xcode project. I am getting a complie error at object.pic = newUser stating Cannot assign value of type ‘User’ to type ‘Data?’. User is the name of my core Data Model. When the code loads it should just save the image literal to the Xcode project. This is a link to gitHub GitHub - redrock34/lebron.
import CoreData
class ViewController: UIViewController {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
lazy var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
override func viewDidLoad() {
super.viewDidLoad()
let newUser = User(context: context)
newUser.pic = #imageLiteral(resourceName: "Jessica").jpegData(compressionQuality: 0.9) as NSObject? as! Data
let object = User(context: context) //create object of type MyEntity
object.pic = newUser //add image to object
do {
try context.save() //save object to CoreData
} catch let error as NSError {
print("\(error), \(error.userInfo)")
}
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
do {
let jake = try context.count(for: fetchRequest)
print("d",jake)
} catch let error as NSError {
print("Could not fetch \(error) ")
}
}}
Checked your project and there are some steps missing in your CoreData process
1.- You need to create NSManagedObject subclasses in your project in order to access the User model
2.- Created a newUser constant with the inferred type of User
3.- The User model have the property of pic defined as Data? but you’re trying to store the previously created newUser constant into it
4.- Since you want to fetch users your NSFetchRequest should be of type User not NSFetchRequestResult otherwise the fetchRequest will result of type Array< Any> instead of Array< User>
Your code was a little hard to follow, but I think I found the issue. First of all you should add all assets to the asset catalog. This allows Xcode to run some optimizations for you and you don’t have to worry about the FileManager which you would have needed to get the image you were referring to. Furthermore you had a typo in your code. #imageLiteral(resourceName: "Jessica") should have been #imageLiteral(resourceName: "jessica"). Always make sure to use the same name everywhere.
Next, you try to set up the same object twice, then you try to assign a User to a property of type Data. This will never work. Long story short, here’s the code to make your project run:
import UIKit
import CoreData
class ViewController: UIViewController {
lazy var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
override func viewDidLoad() {
super.viewDidLoad()
let newUser = User(context: context)
newUser.idx = 1
newUser.pic = #imageLiteral(resourceName: "jessica").jpegData(compressionQuality: 0.9)
do {
try context.save() //save object to CoreData
} catch let error as NSError {
print("\(error), \(error.userInfo)")
}
let fetchRequest: NSFetchRequest<User> = User.fetchRequest()
do {
let jake = try context.count(for: fetchRequest)
print("d",jake)
} catch let error as NSError {
print("Could not fetch \(error) ")
}
}
}
And as @jecht83 mentioned I would also suggest you get the Core Data by Tutorials book.
Hope this helps.