【问题标题】:put retrieved image from coredata to array swift将检索图像从核心数据到数组 swift
【发布时间】:2015-08-03 17:19:18
【问题描述】:

我想将检索到的图像从 CoreData 放到我的 [UImage] 数组中,但实际上这个给了我这个错误:无法将类型“MyApp.Image”(0x7fb4baea09d0)的值转换为“UIImage”(0x10c180758)。

代码如下:

let request = NSFetchRequest(entityName: "ImageVen")
        request.returnsObjectsAsFaults = false

        let result:NSArray = try! context.executeFetchRequest(request)
        if result.count > 0 {
            for picture in result {
                let immagin = picture as! UIImage
                self.immaginiArrayVenerdi.append(immagin)
            }
        }

我该如何解决这个错误?谢谢!

【问题讨论】:

  • 一些想法来解决我的问题?

标签: arrays image swift core-data


【解决方案1】:

当你这样做时:

let result:NSArray = try! context.executeFetchRequest(request)

这意味着result 将是NSManagedObject 的数组。但是你这样做:

        for picture in result {
            let immagin = picture as! UIImage
            self.immaginiArrayVenerdi.append(immagin)
        }

您不能只使用as!NSManagedObject 转换为UIImage,因为它们是完全不同的类,甚至彼此不相似。如果您的实体ImageVen 具有UIImage 的属性,则可以使用该属性的值。但是您不能只告诉编译器进行转换,因为as! 没有提供足够的信息来了解如何进行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2012-05-05
    相关资源
    最近更新 更多