2016-10-01 65 views
0

に私はこれを変更しようとしています:swift3に働く何かにはNSPredicateはSwift3

return [self.allAttributes filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UICollectionViewLayoutAttributes *layoutAttributes, NSDictionary *bindings) { 
     return CGRectIntersectsRect(rect, layoutAttributes.frame); 
     }]]; 

。私は一日中検索されている

return self.allAttributes?.filtered(using: NSPredicate(block: { (layoutAttributes, bindings) -> Bool in 
     return rect.intersects(layoutAttributes.frame) 

    })) as! [UICollectionViewLayoutAttributes]? 

はもちろん、私は

Value of type 'Any?' has no member 'frame'を取得するが、私は解決策を見つけることができません。これまでのところ私はこれを試してみました。

答えて

0

allAttributesNSArrayと宣言されていると仮定します。あなたはこのような何か書くことができるように

はその後、とにかく、あなたは、いくつかのキャストを必要とする:

return self.allAttributes?.filtered(using: NSPredicate(block: { (layoutAttributes, bindings) -> Bool in 
     guard let layoutAttributes = layoutAttributes as? UICollectionViewLayoutAttributes else { 
      print("some thing is wrong...") 
      return false 
     } 
     return rect.intersects(layoutAttributes.frame) 

    })) as! [UICollectionViewLayoutAttributes]? 

をしかし、あなたは唯一のUICollectionViewLayoutAttributesが含まれている場合は、なぜあなたはスウィフトアレイ、[UICollectionViewLayoutAttributes]としてそれを宣言しませんか?

var allAttributes: [UICollectionViewLayoutAttributes] = [] 

//... 

    return allAttributes.filter {rect.intersects($0.frame)} 

この変更に応じていくつかの修正が必要な場合がありますが、一般的にこのような修正を行うとコードがよりシンプルになり、読みやすくなります。

+0

です。ありがとう – mat