Member-only story

Expression implicitly coerced from ‘String?’ to ‘Any’- WHY SWIFT, WHY :(

A common warning in Swift

Steven Curtis
5 min readMar 31, 2020
Photo by Sarah Kilian on Unsplash

Error messages in Swift can be troublesome, particularly if you have a manager who demands that you remove any warnings from your code before committing to your repository.

This article will explain warning Expression implicitly coerced from ‘String?’ to ‘Any’ can be avoided but also how it occurs and why.

Difficulty: Beginner | Easy | Normal | Challenging

Terminology

Any: An instance of any type, including function types

Compiler: A program that converts instructions into a machine-code or lower-level form so that they can be read and executed by a computer

Optional: Swift introduced optionals that handle the absence of a value, simply by declaring if there is a value or not. An optional is a type on it’s own!

String: A collection of Characters

Type Casting: A way to either check the type of an instance, or treat the instance as a different superclass or subclass

Variadric parameters: A parameter for a function that accepts zero or more values of the specified type

The warning

There are a number of variations of this error, but we can take a (relatively) simple example where an optional String is printed.

var str: String? = "This is an optional String"
print (str)

this generates the warning “Expression implicitly coerced from “String?” to ‘Any’ as shown in the following screenshot:

What’s happening for this error message

The easy to understand version

--

--

No responses yet

Write a response