Swift version: 5.2
If you've ever come across the error message "No 'ceil' candidates produce the expected contextual result type 'Int'" – which can happen with calls to
round() – it's usually down to Swift being unable to satisfy type requirements you have asked for.
Put simply, you might think calling
ceil() rounds a floating-point number up to its nearest integer, but actually it doesn't return an integer at all: if you give it a
Float it returns a
Float, and if you give it a
Double it returns a
So, this code works because
c ends up being a
let a = 0.5 let c = ceil(a)
…whereas this code causes your exact issue because it tries to force a
Double into an
Int without a typecast:
let c: Int = ceil(a)
If you need
c to be an integer, the solution is to convert the return value of
ceil() to be an integer, like this:
let c = Int(ceil(a))
The same is true of the
round() functions, so you'd need the same solution.
SPONSORED ViRE offers discoverable way of working with regex. It provides really readable regex experience, code complete & cheat sheet, unit tests, powerful replace system, step-by-step search & replace, regex visual scheme, regex history & playground. ViRE is available on Mac & iPad.
Available from iOS 7.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.