ios - 从 NSOrderedSet 中提取 NSManagedObject 属性值

标签 ios swift swift2

我有一个初始化程序,它接受一个 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/

相关文章:

ios - 如何插入与另一个相关的核心数据记录?

ios - 在字典中添加多个键和值

ios - 检查/比较 CloudKit 中的 NSError 代码?

ios - 如何使用 Locksmith 保存和加载多个凭据?

swift - 使用 JSQMessageViewController 隐藏 AvatarImage 导致白色间隙

iOS 7 - 模式更改的状态栏颜色?

ios - 如何在 Swift 中对子类化的 UIViewController 进行类型转换?

ios - Swift:从类中设置 UITextField.text (从 SQLite 检索值)

Swift 4 UIView.Animate 不触发并忽略动画

ios - Swift:定位 SKCameraNode 的子节点