Ios 将enumerateObjectsUsingBlock转换为Fast Enemuration-Swift
我想知道如何将下面的内容转换成Swift。我曾尝试过,但却被一个概念所束缚: 我使用我的属性UICollectionViewLayoutAttribute在所有对象中循环Ios 将enumerateObjectsUsingBlock转换为Fast Enemuration-Swift,ios,objective-c,swift,enumerate,fast-enumeration,Ios,Objective C,Swift,Enumerate,Fast Enumeration,我想知道如何将下面的内容转换成Swift。我曾尝试过,但却被一个概念所束缚: 我使用我的属性UICollectionViewLayoutAttribute在所有对象中循环 [newVisibleItems enumerateObjectsUsingBlock:^(UICollectionViewLayoutAttributes *attribute, NSUInteger idx, BOOL *stop) { UIAttachmentBehavior *spring = [[UIAtta
[newVisibleItems enumerateObjectsUsingBlock:^(UICollectionViewLayoutAttributes *attribute, NSUInteger idx, BOOL *stop) {
UIAttachmentBehavior *spring = [[UIAttachmentBehavior alloc] initWithItem:attribute attachedToAnchor:attribute.center];
spring.length = 0;
spring.frequency = 1.5;
spring.damping = 0.6;
[_animator addBehavior:spring];
[_visibleIndexPaths addObject:attribute.indexPath];
}];
Swift:标记为*的变量出现错误:
for (index, attribute) in enumerate(newVisibleIems) {
var spring:UIAttachmentBehavior = UIAttachmentBehavior(item: ***attribute***, attachedToAnchor: attribute.center)
spring.length = 0
spring.frequency = 1.5
spring.damping = 0.6
self.animator.addBehavior(spring)
self.visibleIndexPaths.addObject(attribute.indexPath)
}
***类型“AnyObject”不符合协议“UIDynamicItem”
我假设这是因为我没有告诉item属性,该属性是UICollectionViewLayoutAttribute。但我不知道该怎么写
本质上,如何将目标C转换为Swift?只要newVisibleItems声明为
[AnyObject]
,由于Swift严格的类型检查,您的代码就不会编译
要确保newVisibleItems
是预期类型的数组,您必须将for
循环包装在向下广播中:
if let newVisibleItems = newVisibleItems as? [UIDynamicItem] {
for (index, attribute) in enumerate(newVisibleItems) {
...
}
}
如果这个错误的原因在UIKit中,那么请放心,苹果现在正在努力确保其框架的“快速”版本中的每个声明都返回正确的类型,而不是
AnyObject的组合代码>s