如何从一对多关系中正确检索 .allObjects
How to properly retrieve .allObjects from a one to many relationship
我的主页有很多 [Word]
class Page : NSManagedObject {
@NSManaged var words: NSSet
然后我可以通过 :
访问它
let words = self.page.valueForKey("words")
我的错误来自试图将其转换为 [Word]
:
for word in words!.allObjects as! [Word] {
哪个 returns :
error: <EXPR>:1:24: error: 'Word' is ambiguous for type lookup in this context
words!.allObjects as! [Word]
^~~~
Swift.Word:2:18: note: found this candidate
public typealias Word = Int
^
found this candidate
我的理论
我还不太熟悉 Swift/xCode 错误。但这是否试图告诉我 Word
可能在其他地方作为系统对象保留,我不应该使用它?我的另一个理论是,也许我没有正确连接我的实体..因此 Word = Int
.
这是我的话:
这是我的主页:
知道这个错误可能是什么吗?
您正在尝试将集合转换为数组,或者更准确地说是 NSSet
转换为 Array
。此外,当您已经正确地子类化时,您不需要不安全的 valueForKey
。
let words = page.words as Set<Word>
您可以像枚举数组一样枚举它
for word in words { .... }
我的主页有很多 [Word]
class Page : NSManagedObject {
@NSManaged var words: NSSet
然后我可以通过 :
访问它let words = self.page.valueForKey("words")
我的错误来自试图将其转换为 [Word]
:
for word in words!.allObjects as! [Word] {
哪个 returns :
error: <EXPR>:1:24: error: 'Word' is ambiguous for type lookup in this context
words!.allObjects as! [Word]
^~~~
Swift.Word:2:18: note: found this candidate
public typealias Word = Int
^
found this candidate
我的理论
我还不太熟悉 Swift/xCode 错误。但这是否试图告诉我 Word
可能在其他地方作为系统对象保留,我不应该使用它?我的另一个理论是,也许我没有正确连接我的实体..因此 Word = Int
.
这是我的话:
这是我的主页:
知道这个错误可能是什么吗?
您正在尝试将集合转换为数组,或者更准确地说是 NSSet
转换为 Array
。此外,当您已经正确地子类化时,您不需要不安全的 valueForKey
。
let words = page.words as Set<Word>
您可以像枚举数组一样枚举它
for word in words { .... }