xcodeプロジェクトに含まれていないtxtファイルを取得する方法については、ヘルプが必要です。 xcodeプロジェクト内にtxtファイルを持たずにtxtファイルを取得する方法はありますか?ファイルをtxtファイルから読み取っていますが、Xcodeプロジェクト内にはありません
このラインのためにvar data:[[String:String]] = []
var columnTitles:[String] = []
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func reportData(_ sender: Any) {
printData()
}
@IBAction func resetData(_ sender: Any) {
textView.text = "Nope, no Pizza here"
}
@IBAction func readData(_ sender: Any) {
textView.text = readDataFromFile(file: "/Users/mpsip/Desktop/TextFileCSVDemo/data")
}
@IBAction func writeData(_ sender: Any) {
if writeDataToFile(file: "data") {
print("data written")
} else {
print("data not written")
}
}
func cleanRows(file:String)->String{
//use a uniform \n for end of lines.
var cleanFile = file
cleanFile = cleanFile.replacingOccurrences(of: "\r", with: "\n")
cleanFile = cleanFile.replacingOccurrences(of: "\n\n", with: "\n")
return cleanFile
}
func getStringFieldsForRow(row:String, delimiter:String)-> [String]{
return row.components(separatedBy: delimiter)
}
func convertCSV(file:String){
let rows = cleanRows(file: file).components(separatedBy: "\n")
if rows.count > 0 {
data = []
columnTitles = getStringFieldsForRow(row: rows.first!,delimiter:",")
for row in rows{
let fields = getStringFieldsForRow(row: row,delimiter: ",")
if fields.count != columnTitles.count {continue}
var dataRow = [String:String]()
for (index,field) in fields.enumerated(){
dataRow[columnTitles[index]] = field
}
data += [dataRow]
}
} else {
print("No data in file")
}
}
func printData(){
convertCSV(file: textView.text)
var tableString = ""
var rowString = ""
print("data: \(data)")
for row in data{
rowString = ""
for fieldName in columnTitles{
guard let field = row[fieldName] else{
print("field not found: \(fieldName)")
continue
}
rowString += field + "\t"
}
tableString += rowString + "\n"
}
textView.text = tableString
}
//MARK: Data reading and writing functions
func writeDataToFile(file:String)-> Bool{
// check our data exists
guard let data = textView.text else {return false}
print(data)
//get the file path for the file in the bundle
// if it doesnt exist, make it in the bundle
var fileName = file + ".txt"
if let filePath = Bundle.main.path(forResource: file, ofType: "txt"){
fileName = filePath
} else {
fileName = Bundle.main.bundlePath + fileName
}
//write the file, return true if it works, false otherwise.
do{
try data.write(toFile: fileName, atomically: true, encoding: String.Encoding.utf8)
return true
} catch{
return false
}
}
func readDataFromFile(file:String)-> String!{
guard let filepath = Bundle.main.path(forResource: file, ofType: "txt")
else {
return nil
}
do {
// let contents = try String(contentsOfFile: filepath, usedEncoding: String.Encoding.)
let contents = try String (contentsOfFile: filepath, encoding: String.Encoding.utf8)
return contents
} catch {
print ("File Read Error")
return nil
}
}
textView.text = readDataFromFile(ファイル: "/ユーザ/ mpsip /デスクトップ/ TextFileCSVDemo /データ") は、どのように私は自分のアプリケーションやiPadからの正しいファイルパスが何であるかを知っているのですか?
アプリはサンドボックス化されています。あなたのアプリはあなたのMacの道を与えている間、あなたのiPadにありますか?全く意味をなさない。 – Larme
それからギャラリーのファイルパスに関する考えはありますか?また、ファイルパスがサンドボックスになっているか、ユーザーがファイルパスを知ることができないアプリですか?私はちょうど私が私のMacのパスを入力したので、私はランダムに入力するために何を挿入するか分からなかったので、私のMacのパスに関する例を与えている。 –