ios - 如何在 Swift 中指示 for 循环的键是 NSString?

标签 ios core-data for-loop swift

这里我有一个 for 循环。我知道我在其中存储了 NSStrings。如何指示 attributeName 的类型?我不想在循环内到处写 (relationshipName as NSString)

for attributeName in managedObject.entity.attributesByName.keys {
}

更新

如果我添加 [String] 和 for 循环声明,我会收到以下错误:

enter image description here enter image description here

更新

因为String也有问题

enter image description here enter image description here

最佳答案

您必须将 attributesByName 返回的字典转换为真正的键/值类型:

let attributes = managedObject.entity.attributesByName as [String: NSAttributeDescription]
for attributeName in attributes.keys  {
    // attributeName has the type String
    // ...
}

let attributes = managedObject.entity.attributesByName as [NSString: NSAttributeDescription]
for attributeName in attributes.keys  {
    // attributeName has the type NSString
    // ...
}

关于ios - 如何在 Swift 中指示 for 循环的键是 NSString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24968282/

相关文章:

ios - 替换拍摄并重新拍摄按钮UIImagePickerController

ios - 侧面菜单显示不正确

iPhone 。索引属性

java - 无法跳出for(each)循环java

c++ - 在基于范围的 for 循环中获取无效引用

objective-c - 使用 NSDateFormatter 解析日期

ios - 如何使用 Swift Codable 处理部分动态的 JSON?

iphone - 将核心数据信息导出到 Excel 工作表

iphone - 为什么我在此代码中得到 "unknown type name NSManagedObjectContext"?

使用 malloc 在 for 循环中创建链表总是返回相同的地址