【发布时间】:2018-07-30 19:11:55
【问题描述】:
我正在开发一个同时使用 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
....
即使我在“currentFolder?.files”前面添加“(Array)”,我仍然会收到以下错误:
"Cannot assign value of type '(Array<_>).Type' to type '[File]?'"
我在 Swift 方面没有经验,所以任何人都可以帮助我理解为什么这不起作用以及潜在的解决方案吗? (此时我将不得不对所有文件夹的文件进行核心数据提取,但如果我不必这样做,我宁愿不要那么低效)
【问题讨论】:
-
集合和数组是不同种类的集合。见docs.swift.org/swift-book/LanguageGuide/CollectionTypes.html。您是否考虑/尝试过将
filesArray属性设为Set<File>而不是[File]?
标签: objective-c arrays swift core-data