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

CodingChallenges - challenge 27

Forums > Books

Hi everybody

i'm working on the great Coding Challenges Book but i'm struggle with the challenge 27 "Print last lines". We have to add a file to the playground (xcode) with some words then we retrieve the content of this file using contentsOfFile.

I dont really know if i correctly add the file to playground. I have greated a simple file (plain text) that i have added to the Resources file of the playground. Nothing happens. I try with a rtf file added in Ressources or Sources but with no success...

any help?

thank

3      

Hi, any help please?

3      

@twostraws has written a terrific number of articles to help new programmers. Part of the fun of learning is finding the gems that he has sprinkled in and around this fantastic learning site. Maybe finding the answer was part of the syllabus? Something for you to find on your own?

Here's a hint:

See --> How to load a string file...

I just followed these instructions! And now I have working code in a Playground that reads a list of strings from a text file in the resources folder of a Playgrounds file. Awesome!

Give this a try on your own. If you fail, post your code. (Please use the code tags so your code is properly formatted.) Please make sure you comment your code so we can see what you understand and which bits and bobs might be giving you fits.

We're here to help you! But please Help Us to Help You! We need to know how to help you. I think the overall agenda here is helping you learn; it's not to just hand out answers.

3      

thank for our answer. actually, my point was that the solution for this challenge in the book didn't work for me. I precisely followed the first lines of the solution and it didn't work:

func challenge27(filename: String, lineCount: Int) {
   guard let input = try? String(contentsOfFile: filename) else
{ return }

I thought that i haven't correctly created/added the file. I changed with the following and it works:

if let filepath = Bundle.main.path(forResource: filename, ofType: nil) {
    do {
    input = try String(contentsOfFile: filepath)
        print(input)
} catch {
    print("error")
}
}

as it isn't the precise solution of the book, i was wondering

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.