在 IOS 项目中放置 .txt 文件并从中读取的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27206176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:50:23  来源:igfitidea点击:

Where to place a .txt file and read from it in a IOS project

iosxcodeswiftreadfile

提问by Felix Me

i want to put a .txt file in my Xcode Swift Iphone project. First, i simply drag&dropped it from my desktop to the Supporting Files Folder from the project. Isn't there a Folder like on Android "assets", so i can place my files anywhere i want?

我想在我的 Xcode Swift Iphone 项目中放置一个 .txt 文件。首先,我只是将它从我的桌面拖放到项目的支持文件文件夹中。没有像 Android“资产”这样的文件夹,所以我可以将我的文件放在任何我想要的地方?

The file in my example is called README.txt which has a bunch of lines and paragraphs.

我的示例中的文件名为 README.txt,其中包含一堆行和段落。

Simple enough, now I want to print the content of the README.txt file to a view.

很简单,现在我想将 README.txt 文件的内容打印到视图中。

How do i do the read function and what path should I insert, if my file is in the project /SupportFiles/README.txt?

如果我的文件在项目 /SupportFiles/README.txt 中,我该如何执行读取功能以及应该插入什么路径?

Thanks alot!

非常感谢!

回答by Mundi

let path = NSBundle.mainBundle().pathForResource("README", ofType: "txt")
textView.text = String(contentsOfFile: path, 
                             encoding: NSUTF8StringEncoding, 
                                error: nil)

Just drop the file anywhere into the project browser and make sure it is added to the right target.

只需将文件拖放到项目浏览器中的任意位置,并确保将其添加到正确的目标中。

Just to expand on the answer, you can also place them in a folder and use: + pathForResource:ofType:inDirectory:.

只是为了扩展答案,您还可以将它们放在一个文件夹中并使用: + pathForResource:ofType:inDirectory:

回答by Pierre-Yves Guillemet

I would recommend to use NSFileManager and drop your file anywhere in your project :

我建议使用 NSFileManager 并将文件放在项目中的任何位置:

if let path = NSBundle.mainBundle().pathForResource(name, ofType: "txt"){
    let fm = NSFileManager()
    let exists = fm.fileExistsAtPath(path)
    if(exists){
        let c = fm.contentsAtPath(path)
        let cString = NSString(data: c!, encoding: NSUTF8StringEncoding)
        ret = cString as! String
    }
}

回答by Aggressor

Swift 4(thanks to @Pierre-Yves Guillemet for original)

Swift 4(感谢@Pierre-Yves Guillemet 的原创)

As long as the file is in your project (and has a .txt) this will work (in this example, I assume "MyFile.txt" is a file that is in my project):

只要文件在您的项目中(并且有 .txt),这将起作用(在本例中,我假设“MyFile.txt”是我的项目中的一个文件):

static func LoadFileAsString() -> ()
{        
    if let path = Bundle.main.path(forResource: "MyFile", ofType: "txt")
    {
        let fm = FileManager()
        let exists = fm.fileExists(atPath: path)
        if(exists){
            let content = fm.contents(atPath: path)
            let contentAsString = String(data: content!, encoding: String.Encoding.utf8)
        }
    }
}

回答by mokagio

In Swift 3

在斯威夫特 3

guard let path = Bundle.main.path(forResource: "README", ofType: "txt") else {
  return
}

textView.text = try? String(contentsOfFile: path, encoding: String.Encoding.utf8)

回答by fatihyildizhan

If you define path'stype and put !end of the line, you won't get any warning.

如果您定义path's类型并放置!在行尾,您将不会收到任何警告。

 let path:String = NSBundle.mainBundle().pathForResource("README", ofType: "txt")!
 textView.text = String(contentsOfFile: path,
                 encoding: NSUTF8StringEncoding,
                 error: nil)

回答by Kimi Chiu

There's a Assets.xcassetsfor images and files. And here's how to read it from assets.

有一个Assets.xcassets用于图像和文件。这是从资产中读取它的方法。

if let data = NSDataAsset(name: "AssetName")?.data {
    textView.text = String(data: data, encoding: .utf8)
}