我有一个需要读取多个文本文件的简单应用程序。但是现在为了访问文件,我在我的笔记本电脑中给出了文件的地址。
let path = "/Users/alimashreghi/Desktop/glossories/science.txt"
var text:String = ""
do{
text = try NSString(contentsOfFile:path, encoding: NSUTF8StringEncoding) as String
}catch{
}
现在,我担心何时要分发该应用程序。我应该把文本文件放在哪里,我应该如何阅读它们以确保它作为 iPhone 中的应用程序正常工作?
此外,我对分发 swift 应用程序知之甚少。
//
// ViewController.swift
// Test
//
// Created by Ali Mashreghi on 2016-08-29.
// Copyright © 2016 Ali Mashreghi. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
let data = NSData.init(contentsOfFile: "/Users/TestProj/science.txt") //prints Optional(4380)
let data = NSData.init(contentsOfFile: "science.txt") //prints nil
print(data?.length)
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}