Besoin d'aide pour obtenir le fichier txt qui ne se trouve pas dans le projet xcode. Est-il possible de récupérer le fichier txt sans avoir le fichier txt dans le projet xcode?Lecture d'un fichier à partir d'un fichier txt mais pas dans le projet 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
}
}
Pour cette ligne
textView.text = readDataFromFile (fichier: "/ Users/mpsip/Desktop/TextFileCSVDemo/data") comment puis-je savoir quel est le chemin du fichier correct de mon application ou iPad?
Les applications sont en mode bac à sable. Et vos applications sont sur votre iPad, alors que vous lui donnez le chemin de votre Mac? Cela n'a aucun sens. – Larme
Alors avez-vous une idée du chemin du fichier pour la galerie? Et les applications qui sont sandbox quel est leur chemin d'accès ou il ne permet pas aux utilisateurs de connaître le chemin du fichier? Je donne juste un exemple concernant le chemin de mon Mac car je n'avais aucune idée de ce qu'il fallait insérer, donc je suis simplement entré au hasard dans mon chemin Mac à la place. –