0

I'm trying to read text document file with lines in random order after button is pressed. So far I've managed to write a code to read lines with button and display it in label. But problem is that it displays one word and if I click it again it does nothing. Although file is full of words. Solution would be helpful and also code to read lines in random order. Here's my code:

class BeginAfter: UIViewController {

    @IBOutlet weak var word: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func Correct(){
        if let path = Bundle.main.path(forResource: "words", ofType: "txt") {
            do {
                let data = try String(contentsOfFile: path, encoding: .utf8)
                let myStrings = data.components(separatedBy: .newlines)
                word.text = myStrings.joined(separator: "\n")
            } catch {
                print(error)
            }
        }
    }
}
Gurami30
  • 227
  • 8
  • Please, see all the different answers in the linked question. Every answer is for a different situation depending on the location of that file. – Sulthan Jan 31 '22 at 10:56
  • I already seen this answer and it wasn't helpful. And it's also not the answer that I'm looking for – Gurami30 Jan 31 '22 at 10:57
  • If the file is inside your resources then https://stackoverflow.com/a/24098109/669586 from the linked question is *exactly* what you need. In summary, you need the full (absolute) path to the given file. – Sulthan Jan 31 '22 at 11:05
  • I know I tried it many times but it doesn't work. It gives me new errors, tried to fix it but nothing – Gurami30 Jan 31 '22 at 11:13
  • How did you add the file in question to your app? You should open the app bundle for the simulator version of the app and find the file. – Duncan C Jan 31 '22 at 11:26
  • The function `String(contentsOfFile:)` wants a full path to the file. You will need to use one of the Bundle methods that gives you a path to a file of a given type, called on your app's main bundle. – Duncan C Jan 31 '22 at 11:28
  • tried it but it prints out "1". But my file is full of words – Gurami30 Jan 31 '22 at 11:29
  • @Gurami30 However you are not counting words, you are counting lines. Did you try to print `contents` to console? – Sulthan Jan 31 '22 at 11:35
  • It tells me that it can't find it in scope – Gurami30 Jan 31 '22 at 11:39
  • The question makes no sense; it is unrelated to the code. Where is the "lines in random order" part? All you're doing is reading the whole file and returning a number. – matt Jan 31 '22 at 11:51
  • How to do _what_? If you don't know what your own code does, why don't you debug? Why don't you print `contents` as already suggested? Only you know what the file contains; we cannot see it. – matt Jan 31 '22 at 12:00
  • display single words(single word is in single line). And read/display those lines/words in random combination – Gurami30 Jan 31 '22 at 12:02
  • But that is not what your code does. – matt Jan 31 '22 at 12:03
  • I tried to print `contents` but it prints out nothing – Gurami30 Jan 31 '22 at 12:04
  • Yeah I know that it's not doing that. That is why I'm here asking question – Gurami30 Jan 31 '22 at 12:05
  • Okay I changed the code and it displays only one word and if i click it again it does nothing – Gurami30 Jan 31 '22 at 12:18
  • Labels only display one line, by default. That is why we keep suggesting you use `print` to see what your code is doing. – matt Jan 31 '22 at 12:44
  • I printed it out and it printed every single word at once. And they are seperated with `"\n"` – Gurami30 Jan 31 '22 at 12:47
  • So there you go. Your code is working fine. Your code says to read the entire file of words and that is just what happens. – matt Jan 31 '22 at 13:08
  • I want it to print out single words in random not the whole file at once – Gurami30 Jan 31 '22 at 13:10
  • But nothing in your code says to do that. The computer does not know what you "want", it just obeys your instructions. – matt Jan 31 '22 at 13:11
  • Yes I know that my code doesn't does those things. This is why I want some one to help me to write this code that I need – Gurami30 Jan 31 '22 at 13:12
  • We don't write your code for you. What help do you need? Your words are in an array, `myStrings`. Pick a random element of that array. What's the hard part? – matt Jan 31 '22 at 14:10
  • You might try searching. Producing a random word is a pretty common desire, so this sort of question has been asked and answered many times before. For example https://stackoverflow.com/questions/31212658/generate-a-random-word-in-swift – matt Jan 31 '22 at 14:32

0 Answers0