Files

public class Files

Utilities to read files out of the applicaton’s bundle.

  • Loads a file stored in the app’s bundles. It will search each bundle, returning contents of the first matching file. Failures will report an error through a modal alert.

    Declaration

    Swift

    public static func loadFile(named: String) -> String?

    Parameters

    named

    The name of the file to load

    Return Value

    The contents of the file as a string, or Nil if the file does not exist.

  • Prompts the user to select a file from the app bundle that has the desired extension (eg: json, txt, etc.). The action function is called with the name and contents of the selected file if one is chosen. Failures will report an error through a modal alert.

    Usage (inside a ViewController):

    Files.chooseFile(withExtension: "json", forController: self, action: {
      (fileName: String, contents: String) in
        ...
      }
    )
    

    or with trailing closure notation:

    Files.chooseFile(withExtension: "json", forController: self) {
      (fileName: String, contents: String) in
      ...
    }
    

    Declaration

    Swift

    static public func chooseFile(withExtension: String,
                                  forController controller: UIViewController,
                                  action : @escaping (_ fileName: String, _ contents: String) -> Void)

    Parameters

    withExtension

    Extension of all files in bundle to chose from. eg: .json, .txt, …

    forController

    the UIViewController initiating the request.

    action

    The function to call when the user chooses a file. It is passed the name and contents of the chosen file.

    fileName

    the name of the chosen file.

    contents

    the contents of the chosen file as a string.