TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Day 8 - Problem using string.contains("Characters")

Forums > 100 Days of Swift

Hi everyone! Prior to my problem, I want to thank everyone who has contributed in this site! I've been having a blasting time learning Swift with this site!

Okay back to my problem. I got to the topic "Properties and methods of strings" and I encountered problem using string.contains("") -> Bool. Here is the code:

let HelloWorld = "Hello World"

print(HelloWorld.contains("Hello"))

The above code in my Xcode playground running Swift 5.1 comes with this error: cannot convert value of type 'String' to expected argument type 'String.Element' (aka 'Character')

*it is definitely print(HelloWorld.contains("Hello")) in the picture, it looks blurry

3      

let swift = "Hello World"

print(swift.contains("Hello"))

Works fine here. Prints true. Though swift is not a really well chosen variable name. I would change that. I guess you have a typo somewhere ...

3      

Thank you for the reply!

Here is a photo of the error Xcode shows me. It's very strange that it works on yours but not mine. Do you know if there can be any underlying issues with my Xocde?

3      

Strange! I can't reproduce your error here? Is this all of the code?

3      

yes thats all of the codes.

So I just noticed I lied in the beginning, I was using swift playground with swift 5.1, but just now I do the same on xcode, it came out with the same error as well

here's a picture

3      

hi,

the code does work fine, if you import Foundation:

import Foundation
let HelloWorld = "Hello World"
print(HelloWorld.contains("Hello"))

hope that helps,

DMG

4      

Thanks for the reply! That's brilliant I will try it when I get to the topic about importing foundation!

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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.