iOS从文件读取 [英] iOS reading from a file

查看:81
本文介绍了iOS从文件读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的iPhone应用程序将文件中的文本加载到字符串数组中,每个数组元素中的文件只有1行.

I'm trying to get my iPhone app to load text from a file into a string array, with 1 line from the file per array element.

我已经使用崇高的文本将输入文件创建为文本文件.我将文件(位于项目目录内的文件夹内)拖到xCode中,放入项目层次结构中相同位置的文件夹中.

I've created an input file as a text file using sublime text. I dragged the file (which is located inside of a folder inside of my project directory) into xCode into a folder in the same location in the project heirarchy.

我也尝试将其作为捆绑添加(通过复制文件夹并使用.bundle扩展名重命名),无济于事.目前,我的应用程序在2个位置保存了该文件(很显然,我打算删除不需要的版本,但是我不确定该如何运行,因此我暂时将其保留了.)

I also tried adding it as a bundle (by copying the folder and renaming it with the .bundle extension), to no avail. Currently, my app has the file in 2 places (Obviously I plan to delete the unneeded version, but I'm not sure how this will work so I've left it for now).

我编写了一个函数,我想读取我的文件,并将其内容组装到一个数组中:

I've written a function that I want to read my file, and assemble its contents into an array:

func readFromFile(filename: String) -> [String]? {

    guard let theFile = Bundle.main.path( forResource: fileName, ofType: "txt") else {           
        return nil // ALWAYS returns nil here: Seems 'filename' can't be found?????
    }

    do { // Extract the file contents, and return them as a split string array
        let fileContents = try String(contentsOfFile: theFile)
        return fileContents.components(separatedBy: "\n")

    } catch _ as NSError {
        return nil
    }
}

按现状,该函数始终在代码中注释的位置返回nil.

As it stands, the function always returns nil at the location commented in the code.

我已经为此工作了大约6个小时(并尝试了我可以在StackOverflow,google等上找到的每条建议),但我对Swift的各个版本和iOS的复杂性之间的差异越来越感到困惑发展.我似乎在任何地方都找不到一致的答案.我已经查看了Apple文档,但是它的级别太高了,没有示例代码可供我快速入门的初学者理解.

I've been working on this for ~6hrs (and tried every suggestion I could find on StackOverflow, google etc) and I'm just getting more and more confused by the differences between the various versions of Swift and intricacies of iOS development. I can't seem to find a consistent answer anywhere. I've checked the apple documentation but it's too high level with no example code for me to understand at my swift beginner level.

我还尝试用扩展名".txt"命名文件,但这也无济于事.

I also tried naming the file with a ".txt" extension but that didn't help either.

推荐答案

如果要将文件称为forResource: "alert01", ofType: "txt",则必须将文件命名为 alert01.txt .

The file must certainly be named alert01.txt if you are going to refer to it as forResource: "alert01", ofType: "txt".

从捆绑包加载将无法进行.该文件必须是项目的一部分,如第一个条目所示.

Loading from a bundle will not work. The file needs to be part of your project as shown in the first entry.

但是,由于创建了文件夹引用,因此代码无法正常工作.这意味着文件夹 PanicAlertFiles 会与所有内容一起复制到您的分发包中.您的代码将需要深入该文件夹才能检索您的文件.使用 path(forResource:ofType:inDirectory:) 进行此操作,或者(如果您不这样做,不想显式地编码文件名)获取文件夹,然后使用FileManager来检查其内容.

However, your code is not going to work because you have created a folder reference. That means the folder PanicAlertFiles is being copied with all its contents into your bundle. Your code will need to dive into that folder in order to retrieve your file. Use path(forResource:ofType:inDirectory:) to do that, or (if you don't want to have to code the file name explicitly) get the folder and then use the FileManager to examine its contents.

这篇关于iOS从文件读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆