NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

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

   

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

   

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

   

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)

   

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

   

Hacking with Swift is sponsored by Paw

SPONSORED Use Paw to build, test and describe web APIs. Paw has a lightning fast native macOS interface to compose requests, collaborate in real-time on API specs, and generate client code for your applications. You can import and export API definitions.

Discover Paw for Mac

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.