UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Error with code, update required?

Forums > Swift

Working my way through the tests in Day 8 I have come across a few questions which include the use of the contains function:

let song = "Shake it Off"
song.uppercased().contains("SHAKE")

Running this code now throws up the following error:

error: cannot convert value of type 'String' to expected argument type 'String.Element' (aka 'Character') song.uppercased().contains("SHAKE")

Can anyone provide details of an updated approach to searching a string for a substring?

Many thanks,

Simon

3      

Strange, because I just ran that code in a playground and did not get any error.

3      

Thanks for the reply Stuart. I've popped below a screenshot of the error? Thoughts?

3      

Hello Simon!

The code doesn't work because it's missing the line

import UIKit

in the beginning. It needs that line to import the so called Foundation Framework which enables using the String type.

(alternatively using "import Foundation" would also work; when creating a Playground, "import UIKit" should have been in the code automatically)

3      

Thank you @alexmdtu, that seems to have done the trick.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.