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

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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.