GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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 try! Swift Tokyo.

SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!

Get your ticket 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.