I am trying to condense an application I am working on that involves a large directory with lots of UIViewControllers with UITableViews.
My plan is to rewrite the application that will use one-reusuable UIViewController with UITableView that will load in data from the txt files as needed at/after runtime. The txt files will be read at/after runtime instead of having to build all of the swift files before hand.
Currently, my application takes several minutes to build/compile everytime I make a change to the project. Therefore, I am hoping by reading and parsing the txt files as needed, I will be able to build the application within just a few seconds.
Below is my attempt to read and generate the necessary "candies" array using using the example text file provided below (candyTextFile.txt). Please see the commented lines in the class, MasterViewController, for the error message and the code that I am trying to output the candies array as.
Note, I have shortened the lines in my text file down to three lines. My final application will have many of similar text files with each one expanding several hundred lines long.
I am still relatively new to Swift so I apologize if I am missing something simple here and if I am using/not-using the appropraite terminology.
I belive the term CandySearch is just from the project name ( see here ). I am not sure why it is getting outputted though in the candies array. Also, I do see now that my current myArray is established as a class. How can I turn it into an array?
Here is my relevant code in my MasterViewController.swift,
class MasterViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var candies = [myArray]()
var evenIndicies = [String]()
var oddIndicies = [String]()
override func viewDidLoad() {
super.viewDidLoad()
setUpArray()
}
private func setUpArray() {
if let filepath = Bundle.main.path(forResource: "candyTextFile", ofType: "txt") {
do {
// let contents = try String(contentsOfFile: "candyTextFile", encoding: String.Encoding.utf8)
let contents = try String(contentsOfFile: filepath)
let lines_separatedBy_n : [String] = contents.components(separatedBy: "\n")
let string = lines_separatedBy_n.map { String($0) }.joined(separator: ", ")
print("string: \(string)")
let lines_separatedBy_comma : [String] = string.components(separatedBy: ", ")
print("lines_separatedBy_comma: \(lines_separatedBy_comma)")
for (index, element) in lines_separatedBy_comma.enumerated() {
if index % 2 == 0 {
evenIndicies.append(element)
print("evenIndicies: \(evenIndicies)")
} else {
oddIndicies.append(element)
print("oddIndicies: \(oddIndicies)")
}
}
evenIndicies.remove(at: evenIndicies.count - 1) // the -1 removes the nil element, "", in the array. For some reason, there is a fourth element, "", in the evenIndicies array. Therefore, I remove it by subtracting one index so I get the three indexes. My in project txt file is four lines long where the fourth line is empty. I think this is why it is showing up "" for the fourth index.
print("evenIndicies outside for-loop: \(evenIndicies)")
print("oddIndicies outside for-loop: \(oddIndicies)")
candies = [myArray(category: evenIndicies, name: oddIndicies)] //
print("candies: \(candies)")
// This prints as the following:
// candies: [CandySearch.myArray]
// HOWEVER, I am trying to get it to print as:
// [CandySearch.myArray(category: "Chocolate", name: "Chocolate Bar"), CandySearch.myArray(category: "Chocolate", name: "Chocolate Cookie"), CandySearch.myArray(category: "Hard", name: "Lollipop")]
} catch let error as NSError {
print(error.localizedDescription)
}
}
}
}
class myArray {
let category: [String]
let name: [String]
init(category: [String], name: [String]) {
self.category = category
self.name = name
}
}
In my text file, candyTextFile.txt, I have
Chocolate, Chocolate Bar
Chocolate, Chocolate Cookie
Hard, Lollipop
Candyclass. You definecandiesto be an array containingmyArraytypes and then try to populate it with a single instance of typeCandyhence the error message.CandySearchis in your commented print statements? Irrespective, you are still populatingcandieswith a single instance ofmyArraywhich is a class not an array. Your expected print output seems a little off the mark. Thecategoryandnameproperties are not related in any way, beyond being properties of themyArrayinstance. I mean for instance the elements of each array aren’t paired as in your expected print output.CandySearchcomes from and how I can convertmyArrayinto an array to hold the propertiescategoryandname