首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSPredicate in Swift3

NSPredicate in Swift3
EN

Stack Overflow用户
提问于 2016-10-01 21:01:42
回答 1查看 555关注 0票数 0

我正试图改变这一点:

代码语言:javascript
复制
return [self.allAttributes filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UICollectionViewLayoutAttributes *layoutAttributes, NSDictionary *bindings) {
        return CGRectIntersectsRect(rect, layoutAttributes.frame);
        }]];

转换成在swift3中工作的东西。到目前为止,我已经尝试过:

代码语言:javascript
复制
  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'

我已经找了一整天了,但我找不到解决办法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-01 21:21:56

假设您的allAttributes被声明为NSArray

然后,无论如何,你需要一些演员,所以你可以写这样的东西:

代码语言:javascript
复制
    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,为什么不将其声明为Swift,[UICollectionViewLayoutAttributes]

代码语言:javascript
复制
var allAttributes: [UICollectionViewLayoutAttributes] = []

//...

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

根据此更改,您可能需要进行一些修复,但通常情况下,这些修复会使您的代码更加简单和可读性更好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39811445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档