【发布时间】:2015-04-20 17:08:10
【问题描述】:
我的数据模型中有两个实体:学生 > 年级
在 Objective-C 中,我可以使用以下行来链接我的两个对象的关系:
[newStudent setValue:[NSSet setWithObject:newGrade] forKey:@"grades"];
然而在 Swift 中,没有 NSSet。我如何执行相同的操作?
newStudent.setValue(??????, forKey: "grades")
【问题讨论】:
-
当我说“没有 NSSet”时我说错了。我的意思是 Swift 中没有原生集合。然而,正如 gregheo 所指出的,我现在看到 Swift 1.2 添加了一个新的 'Set' 类型。我想要一个使用这个原生 Set 的解决方案。
-
您是否成功使用原生 Swift 'Set' 类型作为 '@NSManaged' 属性?我找不到任何人确认它在网上正常工作。
-
我最终使用了 Martin R 建议的子类。当 Xcode 自动生成子类时,它定义了如下属性:
@NSManaged var gradesRelationship: NSSet然后在我的代码中我使用了 Swift Sets 类似这样的东西:var thisGradeSet:Set<NSObject>? var thisGrade:GradeElement? thisGradeSet = thisStudent!.gradesRelationship.filteredSetUsingPredicate(cellPredicate) var thisScore:Int = thisGrade!.score as Int thisScore = 95 thisGrade!.score = thisScore -
哎呀!对我评论中的格式感到抱歉。
标签: ios swift core-data set nsset