2016-10-01 3 views
0

Ich versuche, dies zu ändern:NSPredicate in swift3

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

in etwas, das in swift3 funktioniert. Bisher habe ich das versucht:

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

    })) as! [UICollectionViewLayoutAttributes]? 

aber natürlich ich Value of type 'Any?' has no member 'frame' bekommen

Ich habe den ganzen Tag gesucht, aber ich kann nicht eine Lösung finden.

Antwort

0

Angenommen, Ihre allAttributes ist als deklariert.

Dann sowieso, müssen Sie einige Casting, so dass Sie so etwas schreiben kann:

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]? 

Aber wenn Sie nur UICollectionViewLayoutAttributes enthalten, warum Sie es als Swift Array nicht erklären, [UICollectionViewLayoutAttributes]?

Sie benötigen möglicherweise einige Korrekturen gemäß dieser Änderung, aber im Allgemeinen solche Fixes machen Ihren Code einfacher und lesbarer.

+0

es funktioniert. Vielen Dank – mat