NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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

   

Hi, any help please?

   

@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.

   

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

   

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 October 1st.

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.