0

I have several locally saved .txt files that are loaded and saved as a URL array like:

func loadShortStories() {
    shortStories = Bundle.main.urls(forResourcesWithExtension: "txt", subdirectory: nil)!
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destinationViewController = segue.destination as? ListVC {
        destinationViewController.shortStories = shortStories
    }
}

And then in the next ListVC where I load them into a tableview, I have:

var shortStories: [URL] = []

I need to hardcode the specific order of the .txt files but need to convert them to a string first. How can I do this?

I tried

let shortStoriesString = shortStories.absoluteString but it does not work.

Added:

I do remove pathextension and lastcomponent in cellForRowAt method with:

shortStories[indexPath.row].deletingPathExtension().lastPathComponent

But before I do that, I want to hardcode the order in which the .txt files are being loaded into the textview because its random and I would like them to be listed in a specific non algorithmic way, like:

let order : [String] = [ "story7.txt", "story3.txt", "story1.txt", "story4.txt" ]

. That's why I am trying to convert the URL into a string.

5
  • What do you mean by locally saved? Where are those files located? Commented Aug 21, 2018 at 0:58
  • In my app bundle Commented Aug 21, 2018 at 1:00
  • I wouldn't convert your URL array to string array. Keep your urls and use them when populating your table view. Regarding your file names you can use FileManager.default.displayName(atPath: yourURL.path) or get your url resourceValues for the localizedNameKey Commented Aug 21, 2018 at 1:20
  • The FileManager.default.displayName(atPath: yourURL.path) will let me hard code the order of .txt files for the tableView? Could you give me a quick example of how to write this? I have never seen this one before. Commented Aug 21, 2018 at 1:31
  • You can custom sort your urls using the url lastPathComponent or the displayName. urls.sorted { $0.lastPathComponent < $1.lastPathComponent } or urls.sorted { FileManager.default.displayName(atPath: $0.path) < FileManager.default.displayName(atPath: $1.path) } Commented Aug 21, 2018 at 2:03

2 Answers 2

1

You can try

let shortStoriesString = shortStories.map {$0.lastPathComponent} 

As shortStories is an array not a single object

Sign up to request clarification or add additional context in comments.

6 Comments

I am not sure but OP probably doesn't want to show the whole file url in his table view. It would be better to just display the url lastPathComponent or get the fileURL resource values for the .nameKey
For those who haven't used map before, it's a way of transforming one array to another one, element by element. In this one, each element is being converted to the result of calling .absoluteString on that same element.
@LeoDabus I think this solution works. The OP wrote that they needed to convert convert a "URL array" to a "string array" and this would definitely be the first step.
Why not just pass the url array itself?
look at the question I would like them to be listed in a specific non algorithmic way, like: let order : [String] = [ "story7.txt", "story3.txt", "story1.txt", "story4.txt" ]
|
0

If you already know the name of the file, use it to generate the url.

let stories: [String] = [ "story7", "story3", "story1", "story4" ]

let storyURL = Bundle.main.url(forResource: stories[index], withExtension: "txt")

1 Comment

Thanks. I just tried this and I get this error - Cannot convert value of type '(Any) -> Int' to expected argument type '(UnboundedRange_) -> ()'

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.