出于某种原因,我无法弄清楚如何将图像保存到核心数据并再次获取它们.我觉得这是我的类型,但看看:
我从api调用我的服务器获取数据.它返回一个base64字符串.这是我获取数据的地方:
updateAccessTokenOnly(newAccessToken: aToken!)
saveImageToDB(brandName: imageBrandName, image: data! )
Run Code Online (Sandbox Code Playgroud)
在这里,我将它保存到我的DB:
func saveImageToDB(brandName: String, image: Data) {
dropImages(){tableDropped in
let managedContext = getContext()
let entity = NSEntityDescription.entity(forEntityName: "CoffeeShopImage", in: managedContext)!
let CSI = NSManagedObject(entity: entity, insertInto: managedContext)
CSI.setValue(image, forKey: "image")
CSI.setValue(brandName, forKey: "brandName")
do {
try managedContext.save()
print("saved!")
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后去取它:
func getImageFromDB(callback: @escaping (_ image: UIImage)-> ()) {
let fetchRequest: NSFetchRequest<NSManagedObject> = NSFetchRequest(entityName: "CoffeeShopImage")
do {
let …Run Code Online (Sandbox Code Playgroud) 因此,当我尝试将部署目标设置为iOS 9时(实际上低于10.0),我会收到标题中所述的错误.
这里存在的问题是:
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "Keebin_development_1")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this …Run Code Online (Sandbox Code Playgroud)