我有一个初始化程序,它接受一个 Strings
数组作为参数。与其重写类并冒着破坏事物的风险,我更愿意为初始化程序提供它想要的东西。
我正在尝试从存储在 NSOrderedSet
中的 NSManagedObjects
中提取 NSStrings
。
这是我尝试过的:
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 中的对象并提取集合中包含的实体的属性值?
最佳答案
我建议阅读有关 KVC(键值编码)的文档,因为您可以将其编写为一行代码:
let filenameArray = soundsToPlay.valueForKey("fileName").array()
对 valueForKey
的调用将返回字符串的 NSOrderedSet
,然后您可以通过调用 array()
将其转换为数组>
关于ios - 从 NSOrderedSet 中提取 NSManagedObject 属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33262883/