0

friends! I previously asked a question Where to place a .txt file and read from it in a IOS project and got a good working answer, which I now want to expand to a little project of mine.

I created a simple TableView Controller which shows me several cells called "Story1", "Story2" and so on.

The story itself should be read from a .txt file which i have in my project "README1.txt", "README2.txt".

The problem which I can't get through is: "How can I show the assigned .txt file in my DetailVC? Maybe the problem is because of my "path" String?

For the story type I created a struct with just name and the path of the file in it as Strings.

import Foundation
struct Story {
var name: String
var path: String? //tested as optional or not, no changes
}



 //Code of the Main `Table View Controller`:
 import UIKit

class MainTVC: UITableViewController {

var data = [Story]()

override func viewDidLoad() {
    // append Stories to Table
    let story1 = Story(name: "TestStory1", path: NSBundle.mainBundle().pathForResource("README1", ofType: "txt")!)
    let story2 = Story(name: "TestStory2", path: NSBundle.mainBundle().pathForResource("README2", ofType: "txt")!)

    data.append(story1)
    data.append(story2)

}
//Tells the data source to return the number of rows in a given section of a table view. 
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return data.count
}

//Asks the data source for a cell to insert in a particular location of the table view.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell

    cell.textLabel.text = "\(data[indexPath.row].name)"
    return cell
}
//Asks the delegate for the height to use for a row in a specified location.
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 75
}

//Notifies the view controller that a segue is about to be performed.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "detail" {
    let row = tableView.indexPathForCell(sender as UITableViewCell)?.row
    (segue.destinationViewController as DetailVC).data = data[row!]
    }
}
}

And the Code from the Detail View Controller:

import UIKit

class DetailVC: UIViewController {

var data: Story?

@IBOutlet weak var storyTV: UITextView!

override func viewDidLoad() {

    //Show the name of the Story as title
    if let name = data?.name {
        title = name
    }
    else {
        title = "No Story"
    }

    //tried the following lines to get the Story shown
    let pathx = data?.path
    storyTV.text = String(contentsOfFile: data?.path!, encoding: NSUTF8StringEncoding, error: nil) //doesn't work and error occuring because of "data?.path!" just says add "!" to "path" and if i do, it says remove "!". Don't know why?

    storyTV.text = String(contentsOfFile: pathx!, encoding: NSUTF8StringEncoding, error: nil) // doesn't work to show, but don't know why
}
}
Community
  • 1
  • 1
Felix Me
  • 481
  • 1
  • 4
  • 10

1 Answers1

0

Here the answer to my question which i found out by myself through several println(). I printed out the path from the class DetailVC and it was always empty or the string beginning was Optinal("...)". So in the end it was just a misunderstanding of Optionals.

For readers with the same problem of opening text files with variables, here the corrected code of the last 3 lines from the DetailVC class:

if let path = data?.path {
        data?.path = path
        storyTV.text = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)
    }
    else {
        data?.path = "empty"
    } 

Good luck with it.

Felix Me
  • 481
  • 1
  • 4
  • 10