快速将二进制数据提取到索引数组中

问题描述 投票:1回答:1

下面的我的快速代码试图获取一个编号的核心数据二进制数组。我收到错误消息:无法将类型为“ UIImage”的值分配为类型为“数据?”在piczy [I]。如果我从字符串中获取核心数据,但是使用图像执行此代码,则此代码将无效,并会导致编译错误。用户是核心数据实体的名称。

enter image description here

  class ViewController: UIViewController{
   var piczy = [UIImage]()
    func fetch()
    {

          for i in 0..<piczy.count {
            let newUser = Users(context: context)
            newUser.image = piczy[i]
            newUser.idxPic = Int32(i + 1)

        }
  }}
arrays swift core-data indexing uiimageview
1个回答
0
投票

错误是说newUser.image的类型为Data?,但是您的元素piczy[i]的类型为UIImage。您可以尝试newUser.image = piczy[i].pngData()

© www.soinside.com 2019 - 2024. All rights reserved.