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      

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.