在将项目迁移到swift 5之后,我得到了很多错误,例如
Expression implicitly coerced from 'UIButton?' to 'Any'我不知道是什么导致了这一切。发生这种情况的一个例子(有一堆)是当我设置view.accessibilityElements时。数组应该包含:有吗.知道是什么导致的吗?
下面是一个示例:
@IBOutlet weak var shareButton: UIButton!
@IBOutlet weak var shareTitleLabel: UILabel!
view.accessibilityElements = [shareButton, shareTitleLabel]下面是另一个例子:
@IBOutlet weak var titleLabel: UILabel!
let titleConstraints = [
NSLayoutConstraint(item: titleLabel, attribute: .leading, relatedBy: .equal, toItem: otherView, attribute: .leading, multiplier: 1, constant: horizontalTextInset),
NSLayoutConstraint(item: titleLabel, attribute: .trailing, relatedBy: .equal, toItem: otherView, attribute: .trailing, multiplier: 1, constant: -horizontalTextInset)
]当像这样设置上面的元素时,它会导致上述错误。
发布于 2019-03-30 04:27:30
以下是几点意见:
foo }做一些事情
那么,看看这段代码,foo是可选的还是未包装的值?在Swift 5中,它用这个警告引起了我们的注意。
警告:从“UIButton?”中隐式胁迫的表达式?转到“任何”它将向您展示消除这种模糊性的三种可能的自动修复方法,即:
- use `nil`-coalescing operator, `??`;
- force unwrap it, `!`; or
- just cast it with `as Any` to explicitly say that `foo` will be the optional with no unwrapping.总之,我们希望能够轻松地对代码进行推理,而Any类型只会使这一点变得模糊。编译器不再假设您是否希望打开button,而是要求我们将我们的意图明确化。
foo将是可选的:
让福: UIButton?=按钮UIButton!插座被看作是UIButton? (而不是作为ImplicitlyUnwrappedOptional类型,或者即使使用Any类型也会自动强制展开),那么在隐式解包装选项的重新实现和SE-0054废除ImplicitlyUnwrappedOptional型中有一些有趣的讨论。https://stackoverflow.com/questions/55422914
复制相似问题