ios - 在 Swift 中迭代 NSLayoutConstraints 会出现 "[AnyObject] does not have a member named ' Generator'"错误

标签 ios swift

我需要迭代我的 View 的 super View 的约束。 view.superview.constraints 不是可选的,但我仍然收到“[AnyObject] 没有名为“Generator”的成员”错误。这是我的代码

 func findViewHeightConstraint() {
    if let sv = self.view.superview {
        for constraint in sv.constraints {

        }
    }
}

View 约束定义为

   func constraints() -> [AnyObject

所以不是可选的。尝试转换为 [NSLayoutConstraints] 会产生永远不会成功的警告。关于如何使用 Swift 迭代 View 约束有什么想法吗?

最佳答案

我犯了一个非常愚蠢的错误。自动完成功能一直试图告诉我一些事情,结果是

 func findViewHeightConstraint() {
    if let sv = self.view.superview {
        for constraint in sv.constraints() {

        }
    }
}

关于ios - 在 Swift 中迭代 NSLayoutConstraints 会出现 "[AnyObject] does not have a member named ' Generator'"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31666983/

相关文章:

iphone - Corebluetooth 连接如何在连接时节省电池电量

ios - 在 XCode 和 IOS 7 中移动文件

ios - 使用动态字典创建可扩展 TableView

ios - 如何使用与 NewtonSoft (JSON.Net) 组件中的 JSON 匹配的 Swift 类从 JSON 读取/写入对象数组?

swift - 更新框架 xcode8 后构建错误

android - 更改整个 React Native App 的语言

ios - 如何根据用户是否登录来处理在应用程序启动时显示不同的 View Controller ?

swift - 如何在 MessageUI (Swift 4) 中启用按钮

ios - 按多个键值对数据进行排序

swift - 如何在 SwiftUI 中使用 Picker 设置文本字段作为输入