ClémentFvr
ClémentFvr

Reputation: 115

Migrating from swift 3 to swift 4

I'm working on a project migrating from Swift 3 to 4. I fixed all the issues except this one. First at all I don't understand this code :

let messageText = stride(from: 0, to: lineNumber, by: 1).reduce("") { "\n\($0.0)" }

Reduce function has changed between these two versions. So I want to re-write it to Swift 4.

Upvotes: 1

Views: 110

Answers (1)

Martin R
Martin R

Reputation: 539685

Assuming that the purpose is to create a string with the numbers from 0 (inclusive) to lineNumber (exclusive), separated by newline characters, then it should be

let messageText = stride(from: 0, to: lineNumber, by: 1).reduce("") { "\($0)\n\($1)" }

The error message is misleading, the real problem is that the closure has the wrong type, it must take two arguments.

The same can be achieved with

let messageText = (0..<lineNumber).map(String.init).joined(separator: "\n")

Upvotes: 1

Related Questions