Objective c 无法分配类型为';(数组<;&ugt;).Type';致[海关类]

Objective c 无法分配类型为';(数组<;&ugt;).Type';致[海关类],objective-c,arrays,swift,core-data,Objective C,Arrays,Swift,Core Data,我正在从事一个使用Objective-C(旧代码)和Swift(新代码和将来添加的任何代码)的项目 我在CoreData模型中创建了两个新实体,我们称它们为文件夹和文件。文件夹与文件具有多对多关系 以下是我所提到的自动生成子类的代码: @interface Folder (CoreDataProperties) + (NSFetchRequest<Folder *> *)fetchRequest; .... @property (nullable, nonatomic, retai

我正在从事一个使用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
因此,我尝试将特定文件夹记录的文件设置为该属性:

//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
....
//其他一些Swift文件
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
...
将cell=tableView.dequeueReusableCell(标识符为:FilesTableViewCellIdentifier)设为!FilesTableViewCell
让currentFolder=folderArray.last
cell.filesArray=currentFolder?.files
//当我收到“无法将'Set'类型的值赋给'[File]?'类型”错误时,该行不起作用
返回单元
....
即使我将“(数组)”添加到“currentFolder?.files”的前面,仍然会出现以下错误:

"Cannot assign value of type '(Array<_>).Type' to type '[File]?'"
“无法将类型'(数组)的值赋值给类型'[文件]?”

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

请注意,
文件
不是一个
数组
,而是一个
集合
。集合与数组的不同之处在于它没有顺序,因此当您遍历
集合
时,顺序可以(将)每次都要不同。
Set
但是要确保一个对象只能添加一次,因此,如果将两个相同的对象添加到
Set
中,它将只包含一个对象,因此不会重复

要从
Set
获取
数组
,只需执行
Array(yourSet)
即可,如果您的Set是
类型,Set
数组将是
[文件]

您只需将代码更改为:

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
    ....
}

集合和数组是不同类型的集合。请参阅。您是否考虑过/尝试过将
filesArray
属性设置为
Set
而不是
[File]
?谢谢你,这个解决方案工作得很好。仍然不确定为什么首先执行if-let语句可以解决将其转换为数组的问题,但我会解决作为临时修复程序的低效核心数据获取问题。它不会将其转换为数组!,
让filesArray=array(files)
这就是将
类型的
文件
设置为
[文件]类型的数组的原因
我倾向于使用
if-let
而不是optionals,因为它更容易阅读,而且你知道只有当某些值存在并且对于optionals不
nil
时,你才会做一些事情……不,我的意思是if-let允许我以后将其转换为数组。无论如何,再次感谢你的帮助,非常感谢你的老板,是的,eit使用强制展开执行
数组(currentFolder!.files!)
时,她使用了这种或更不安全的方式,而不是您想要执行的操作
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
    ....
}