Swift 6 change to closure and throws

I try to read all changes that happened in Swift 6 before asking here, but I did not found an answer.

The following code works in Swift 5

extension UIView{
   func addSubviews(_ views : [UIView]) {
      views.forEach(self.addSubview(_:))
   }
}

But for Swift 6 I get:
Call can throw, but it is not marked with 'try' and the error is not handled

The fix is easier

func addSubviews(_ views : [UIView]) {
    views.forEach({ self.addSubview($0) })
}

But I would like to know what changed so I can avoid it in the future.

The only thing Im changing is the SWIFT_VERSION setting in my project using Xcode 16.

2 Likes

I can reproduce it. It looks like to be a compiler bug for me.

I suggest you create an issue on swiftlang/swift.

2 Likes

For anyone wanting to know the outcome of this, here is the Swift issue.

Thanks @Kyle-Ye

3 Likes