代码之家  ›  专栏  ›  技术社区  ›  Tom A

无法将“”类型的值(array<\>).type“”赋给[customClass]

  •  0
  • Tom A  · 技术社区  · 7 年前

    我正在开发一个同时使用Objective-C(旧代码)和Swift(新代码和将来添加的任何代码)的项目。

    我在coredata模型中创建了两个新实体,我们称它们为文件夹和文件。文件夹与文件有一个对多关系。

    以下是我迄今为止所提到的自动生成子类的代码:

    @interface Folder (CoreDataProperties)
    
    + (NSFetchRequest<Folder *> *)fetchRequest;
    ....
    @property (nullable, nonatomic, retain) NSSet<File *> *files;
    ....
    @end
    
    @interface File (CoreDataProperties)
    
    + (NSFetchRequest<File *> *)fetchRequest;
    ....
    @property (nullable, nonatomic, retain) Folder *folder;
    ....
    @end
    

    我正在处理swift文件中的一个文件夹记录,我只是试图在另一个具有folder.files关系的页面上设置一个属性。

    这是我试图设置的另一页(swift)上的属性:

    class FilesTableViewCell: UITableViewCell {
    
      ...
      var filesArray: [File]? = []
    ...
    }
    

    因此,我尝试将特定文件夹记录的文件设置为该属性:

    //some other Swift file
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      ...
      let cell = tableView.dequeueReusableCell(withIdentifier: FilesTableViewCellIdentifier) as! FilesTableViewCell
      let currentFolder = folderArray.last
      cell.filesArray = currentFolder?.files 
      // the line does not work I get a "Cannot assign value of type 'Set<File>?' to type '[File]?'" error
      return cell
    ....
    

    即使在“当前文件夹”前面添加“(array)”时也是如此。.files“我仍然得到以下错误:

    "Cannot assign value of type '(Array<_>).Type' to type '[File]?'"
    

    我在斯威夫特没有经验,所以有人能帮助我理解为什么这不起作用和一个潜在的解决方案吗?(此时,我将不得不对所有文件夹的文件进行核心数据提取,但如果不必这样做,我宁愿没有效率。)

    1 回复  |  直到 7 年前
        1
  •  1
  •   Ladislav    7 年前

    files Array Set

    Array(yourSet) Set<File> [File]

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        ...
        let cell = tableView.dequeueReusableCell(withIdentifier: FilesTableViewCellIdentifier) as! FilesTableViewCell
    
        if let currentFolder = folderArray.last, let files = currentFolder.files {
            let filesArray = Array(files)
            cell.filesArray = filesArray
        }
    
        return cell
        ....
    }