【发布时间】:2019-01-03 03:59:02
【问题描述】:
我正在尝试压缩我正在处理的一个应用程序,该应用程序涉及一个包含大量UIViewControllers 和UITableViews 的大目录。
我的计划是重写应用程序,该应用程序将使用可重复使用的UIViewController 和UITableView,它将在运行时/运行后根据需要从 txt 文件中加载数据。 txt 文件将在运行时/之后读取,而不必事先构建所有 swift 文件。
目前,每次我对项目进行更改时,我的应用程序都需要几分钟来构建/编译。因此,我希望通过根据需要读取和解析 txt 文件,我将能够在几秒钟内构建应用程序。
下面是我尝试使用下面提供的示例文本文件 (candyTextFile.txt) 来读取和生成必要的“糖果”数组。 请查看课程中的注释行 MasterViewController,以获取错误消息和我尝试将糖果数组输出为的代码。
注意,我已将文本文件中的行数缩短为三行。我的最终应用程序将包含许多类似的文本文件,每个文件都扩展数百行。
我对 Swift 还比较陌生,所以如果我在这里遗漏了一些简单的东西,或者我使用/不使用适当的术语,我深表歉意。
我相信 CandySearch 一词仅来自项目名称(请参阅 here )。我不确定为什么它会在candies 数组中输出。另外,我现在确实看到我当前的myArray 已建立为一个类。如何把它变成一个数组?
这是我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
}
}
在我的文本文件中,candyTextFile.txt,我有
Chocolate, Chocolate Bar
Chocolate, Chocolate Cookie
Hard, Lollipop
【问题讨论】:
-
请出示
Candy类的代码。您将candies定义为包含myArray类型的数组,然后尝试使用Candy类型的单个实例填充它,因此出现错误消息。 -
哦,哇,这对我来说是个大错误。请查看更新的代码!谢谢大家的帮助。
-
对不起,如果我在之前的评论中不清楚。问题仍未解决。
-
您能否更新您的答案以删除/解释您评论的打印声明中的
CandySearch是什么?无论如何,您仍然使用myArray的单个实例填充candies,这是一个类而不是数组。您预期的打印输出似乎有点偏离标准。除了myArray实例的属性之外,category和name属性没有任何关系。我的意思是,例如每个数组的元素并没有像您预期的打印输出那样配对。 -
好的,我在原始帖子的中间附近添加了一段关于我认为
CandySearch的来源以及如何将myArray转换为一个数组来保存属性category和@ 987654348@
标签: swift uitableview parsing uiviewcontroller uisearchcontroller