如何在Swift中指示for循环的键是NSString? [英] How can I indicate in a Swift that key of for loop is NSString?
本文介绍了如何在Swift中指示for循环的键是NSString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里我有一个for循环。我知道我存储NSStrings在它。如何指示 attributeName
的类型?我不想写循环中的任何地方,(relationshipName as NSString)
。
code>
UPDATE
如果我添加 [String]
和for循环声明,我得到以下错误: p>
>
UPDATE
as String也有问题
解决方案
您必须将 attributesByName
返回的字典转换为实际键/值类型:
let attributes = managedObject.entity.attributesByName as [String:NSAttributeDescription]
attribute.keys中的attributeName {
// attributeName的类型为String
// ...
}
或
let attributes = managedObject.entity.attributesByName as [NSString:NSAttributeDescription]
for attributeName in attributes.keys {
// attributeName的类型为NSString
// ...
}
Here I have a for loop. I know that I store NSStrings in it. How can I indicate the type of attributeName
? I do not want to write everywhere inside the loop, that (relationshipName as NSString)
.
for attributeName in managedObject.entity.attributesByName.keys {
}
UPDATE
If I add [String]
and of the for loop declaration, I get the following error:
UPDATE
as String has also problem
解决方案
You have to cast the dictionary returned by attributesByName
to the real key/value type:
let attributes = managedObject.entity.attributesByName as [String: NSAttributeDescription]
for attributeName in attributes.keys {
// attributeName has the type String
// ...
}
or
let attributes = managedObject.entity.attributesByName as [NSString: NSAttributeDescription]
for attributeName in attributes.keys {
// attributeName has the type NSString
// ...
}
这篇关于如何在Swift中指示for循环的键是NSString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文