问题描述
我正在尝试加载具有以下格式的.txt文件
人员 位置 数据 平均值 目标
第一人称董事37 45 80
第二个人助理23 56 34
第三个人CEO 34 45 67
共有五列,第一行是标题。以下是我在viewDidLoad中使用的代码:
<nav class="main-nav">
<ul class="main-nav-list">
<li><a href="#" class="main-nav-link">Books</a></li>
</li>
<li>
<a href="#" class="main-nav-link">About</a>
</li>
<li>
<a href="#" class="main-nav-link">Contact</a>
</li>
</ul>
</nav>
.main-nav{
grid-column:4/-1;
grid-row:3/4;
position:relative;
}
.main-nav-list{
display:grid;
grid-template-columns: repeat(6,1fr);
grid-template-rows: repeat(3,1fr);
position:relative;
}
.main-nav-link{
text-decoration:none;
text-transform:uppercase;
color:darkolivegreen;
display:grid;
}
.main-nav-link::before,.main-nav-link::after{
content:"";
width:90%;
height:4px;
display:block;
position:absolute;
}
但是,我在下面看到错误消息
override func viewDidLoad() {
super.viewDidLoad()
// load txt file from dropBox
let url = URL(string: "https://www.dropBox.com/s/pr0ldvdeab48mpp/prueba.txt?dl=0")
let task = URLSession.shared.downloadTask(with: url!) {(urlresponse,response,error) in
guard let originalURL = urlresponse else { return }
do {
// get path to directory
let path = try FileManager.default.url(for: .documentDirectory,in: .userDomainMask,appropriateFor: nil,create: false)
// giving name to file
let newUrl = path.appendingPathComponent("myTextFile")
// move file from old to new url
try FileManager.default.moveItem(at: originalURL,to: newUrl)
} catch {
print(error.localizedDescription)
return
}
}
task.resume()
//reading the file
let path = FileManager.default.urls(for: .documentDirectory,in: .userDomainMask).first
do {
let fileUrls = try FileManager.default.contentsOfDirectory(at: path!,includingPropertiesForKeys: nil)
//read the text
let textContent = try String(contentsOf: fileUrls[0],encoding: .utf8)
print(textContent)
} catch {
print("ERROR")
}
}
线程1:致命错误:索引超出范围
我的目标是能够在需要时单独读取文件和访问每列/行的元素,但不能识别问题的根源。感谢任何帮助。
注意:如果有帮助,我可以灵活地将文件加载为.csv或其他格式(如果更容易的话)。
解决方法
您的问题是dl = 0并不指向文件。它将指向一个html文件。您需要使用dl = 1才能直接下载文件。您还应该将下载代码从下载任务移到完成封包中。您还可以从url响应中获取文件名。尝试这样:
// load txt file from dropbox (make sure you change the string suffix from dl=0 to dl=1)
let url = URL(string: "https://www.dropbox.com/s/pr0ldvdeab48mpp/prueba.txt?dl=1")!
let task = URLSession.shared.downloadTask(with: url) { location,response,error in
guard let location = location else { return }
do {
// get the documentDirectory url
let documentDirectory = try FileManager.default.url(for: .documentDirectory,in: .userDomainMask,appropriateFor: nil,create: false)
print(documentDirectory.path)
// get the suggested name fro the url response or the url last path component
let name = (response as? HTTPURLResponse)?.suggestedFilename ?? location.lastPathComponent
// create a destination url
let destination = documentDirectory.appendingPathComponent(name)
// check if the file already exists
if FileManager.default.fileExists(atPath: destination.path) {
// remove the file (if you would like to use the new one)
try FileManager.default.removeItem(at: destination)
}
// move file from the temporary location to the destination
try FileManager.default.moveItem(at: location,to: destination)
// reading the downloaded file
let textContent = try String(contentsOf: destination,encoding: .utf8)
print(textContent)
} catch {
print("ERROR")
}
}
task.resume()