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

Unwrap: Array of Terries

Forums > 100 Days of Swift

Hello, I have a problem with one task in Unwrap app.

"Write code that creates an array containing all the people with the first name Terry."

And I write something like this:

var names = ["Terry Jones", "John Terry", "Terry Crews", "Terry Farrell", "Samuel Terry"]
var terries = [String]()
for name in names {
    if name.hasPrefix("Terry") {
        terries.append(name)
    }
}

In playgorund my code works fine, but Free Coding in Unwrap won't accept this solution.

Where is my bad?

Thanks for help!

3      

Try

var names = ["Terry Jones", "John Terry", "Terry Crews", "Terry Farrell", "Samuel Terry"]
var terries: [String] = [String]()
for name in names {
    if name.hasPrefix("Terry") {
        terries.append(name)
    }
}

4      

It works! Thank you very much!!

But can you explain me why? and why my code is wrong?

Thanks.

3      

Your is not wrong. It just that the "Unwrap" looks for certain "answers" and infering a type it does not do (at the moment).

To be honest. I had a look at the "Unwrap" code at the json file

3      

Okay, Thanks again for help! problem solved :)

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free 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.