我有一个初始化器,它以Strings数组作为参数。与其重写类并冒险破坏类,我更愿意按它的要求提供初始化器。
我正在尝试从存储在NSStrings中的NSManagedObjects中提取NSOrderedSet。
以下是我尝试过的:
let soundsToPlay = sequenceToPlay.sounds as NSOrderedSet
for element in soundsToPlay {
// this prints out the object in console
print("\(element)")
// this, doesn't give me accessors for the sound object
// print("\(element.fileName)")
}我错过了一些基本的东西,但我不知道是什么。如何枚举NSOrderedSet中的对象并提取集合中包含的实体的属性值?
发布于 2015-10-21 15:15:02
我建议阅读关于KVC (键值编码)的文档,因为您可以将其写成一行代码:
let filenameArray = soundsToPlay.valueForKey("fileName").array()对valueForKey的调用将返回字符串的NSOrderedSet,然后可以通过调用array()将其转换为数组。
发布于 2015-10-21 15:14:20
我想通了。我错过了一步:
let soundsToPlay = sequenceToPlay.sounds as NSOrderedSet
for element in soundsToPlay {
// I have to tell the compiler what type of thing my thing is
let whatIWantToAccess = element as! MyObjectINeedAccessorsFor
print("\(whatIWantToAccess.fileName)")
}发布于 2015-10-21 15:14:43
这可能是因为编译器认为“元素”是NSManagedObject的实例,并且它没有fileName,尝试显式类型转换,类似于
for element: YourClass in soundsToPlay https://stackoverflow.com/questions/33262883
复制相似问题