0

I have a swift class that reads lines from a text document and prints out the first line. After, every time a button is clicked a new line is read out.

What I want is to have a random line printed out the first time, and then a random line printed out after every button click.

Here's what I have so far:

import Foundation
import UIKit

class InfoController: UIViewController {

// MARK: Properties
@IBOutlet weak var difficultylevel: UILabel!
var i:Int = 0


override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func readFile(){
    if let path = NSBundle.mainBundle().pathForResource("easymath", ofType: "txt"){
        var data = String(contentsOfFile:path, encoding: NSUTF8StringEncoding, error: nil)

        if let content = data {
            let myStrings = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())


            let randomIndex = Int(arc4random_uniform(UInt32(myStrings.count)))
            difficultylevel.text = myStrings[randomIndex]
        }
    }
}



@IBAction func difficultybutton(sender: UIButton) {

    difficultylevel.text = // TODO insert random index of "myStrings" array here

}
}

However, I cannot access the myStrings array at the TODO portion inside the button click. Any help on how to set this up?

2
  • 1
    Make myStrings a property instead of a local variable (and learn about scope). Commented May 25, 2016 at 14:16
  • 1
    You'll also want to call readFile() at some point. Perhaps in viewDidLoad Commented May 25, 2016 at 14:20

1 Answer 1

1

Variable scope in Swift is limited to the brackets of the function. So to make myStrings available outside of your readFile() function, you need to declare it as a property for the class:

@IBOutlet var difficultyLevel: UILabel? // BTW your IBOutlet should not be weak
var i: Int = 0
var myStrings: [String]?

Since you are going to use the random functionality over and over, we can abstract the function like this

func randomString() -> String? {
     if let strings = myStrings {
         let randomIndex = Int(arc4random_uniform(UInt32(myStrings.count)))
         return strings[randomIndex]
    }
    return nil
}

then your instantiation will be like this

if let content = data {
    myStrings = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())
    difficultyLevel.text = randomString()
}

Then, your difficultybutton function will be (with an abstracted random string function)

// Changed the name for better readibility
@IBAction func difficultyButtonTapped(sender: UIButton) {
     difficultyLevel.text = randomString()
}

Finally, there isn't any code that calls the readFile function, so you should add it to probably the viewDidLoad function as @CharlesCaldwell points out

override func viewDidLoad() {
     super.viewDidLoad()
     readFile()
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.