【发布时间】:2014-09-02 15:18:56
【问题描述】:
我尝试在 Swift 中打开文件。为此,我创建了文件路径。它不起作用。
maaaacy:~ $ pwd
/Users/tsypa
maaaacy:~ $ cat a.txt
test
maaaacy:~ $ ./a.swift
nil!
maaaacy:~ $
脚本:
#!/usr/bin/xcrun swift
import Foundation
var filePath = NSBundle.mainBundle().pathForResource("a", ofType: "txt", inDirectory: "/Users/tsypa")
if let file = filePath {
println("file path \(file)")
// reading content of the file will be here
} else {
println("nil!")
}
怎么了?
【问题讨论】:
-
问题是该文件不属于“捆绑包”。请改用
NSFileManager(或NSURL,如果您只需要引用资源的路径)。