我怎么能在飞快的操场上看文件

时间:2023-01-23 16:37:35

Im trying to read a text file using a Swift playground with the following

我试着在一个快速的操场上阅读一个文本文件。

let dirs : String[]? =    NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) as? String[]

if (dirs != nil) {
    let directories:String[] = dirs!;
    let dir = directories[0]; //documents directory
    let path = dir.stringByAppendingPathComponent(file);

    //read
    let content = String.stringWithContentsOfFile(path, encoding: NSUTF8StringEncoding, error: nil)
}

However this fails with no error. It seems the first line stops the playground from outputting anything below

然而,这没有错误。第一行似乎阻止了操场输出任何东西

8 个解决方案

#1


4  

This works for me. The only thing I changed was to be explicit about the file name (which is implied in your example) - perhaps you have a typo in the off-screen definition of the "file" variable?

这适合我。我唯一更改的是文件名称的显式(在您的示例中包含)——也许您在“file”变量的屏幕外定义中有一个错误?

let dirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) as? [String]

let file = "trial.txt" // My change to your code - yours is presumably set off-screen
if let directories = dirs {
  let dir = directories[0]; //documents directory
  let path = dir.stringByAppendingPathComponent(file);

  //read
  let content = NSString(contentsOfFile: path, usedEncoding: nil, error: nil)
  // works...
}

#2


49  

You can also put your file into your playground's resources. To do this: show Project Navigator with CMD + 1. Drag and drop your file into the resources folder. Then read the file:

你也可以把你的档案放在你的操场的资源。为此:使用CMD + 1显示项目导航器。将您的文件拖放到资源文件夹中。然后读取文件:

On XCode 6.4 and Swift 1.2:

关于x6.4和Swift 1.2:

var error: NSError?
let fileURL = NSBundle.mainBundle().URLForResource("Input", withExtension: "txt")
let content = String(contentsOfURL: fileURL!, encoding: NSUTF8StringEncoding, error: &error)

On XCode 7 and Swift 2:

关于XCode 7和Swift 2:

let fileURL = NSBundle.mainBundle().URLForResource("Input", withExtension: "txt")
let content = try String(contentsOfURL: fileURL!, encoding: NSUTF8StringEncoding)

On XCode 8 and Swift 3:

关于XCode 8和Swift 3:

let fileURL = Bundle.main.url(forResource: "Input", withExtension: "txt")
let content = try String(contentsOf: fileURL!, encoding: String.Encoding.utf8)

If the file has binary data, you can use NSData(contentsOfURL: fileURL!) or Data(contentsOf: fileURL!) (for Swift 3).

如果文件有二进制数据,您可以使用NSData(contentsOfURL: fileURL!)或data (contentsOf: fileURL!)(用于Swift 3)。

#3


26  

While the answer has been supplied for a quick fix, there is a better solution.

虽然已经提供了快速修复的答案,但还有更好的解决方案。

Each time the playground is opened it will be assigned a new container. This means using the normal directory structure you would have to copy the file you want into the new container every time.

每次打开操场,就会给它分配一个新的容器。这意味着要使用正常的目录结构,每次都要将您想要的文件复制到新容器中。

Instead, inside the container there is a symbolic link to a Shared Playground Data directory (/Users//Documents/Shared Playground Data) which remains when reopening the playground, and can be accessed from multiple playgrounds.

相反,在容器内部有一个符号链接,链接到共享的操场数据目录(/用户//文档/共享的操场数据),在重新打开操场时,可以从多个操场访问。

You can use XCPlayground to access this shared folder.

您可以使用xc游乐场来访问这个共享文件夹。

import XCPlayground

let path = XCPlaygroundSharedDataDirectoryURL.appendingPathComponent("foo.txt")

The official documentation can be found here: XCPlayground Module Reference

官方文档可以在这里找到:xc游乐场模块参考

Cool post on how to organize this directory per-playground: Swift, Playgrounds, and XCPlayground

关于如何组织这个目录的酷帖子:斯威夫特,操场,和xc操场。

#4


10  

Swift 3 (Xcode 8)

斯威夫特3(Xcode 8)

The code below works in both iOS and macOS playgrounds. The text file ("MyText.txt" in this example) must be in the Resources directory of the playground. (Note: You may need to open the navigator window to see the directory structure of your playground.)

下面的代码可以在iOS和macOS上运行。文本文件(“MyText。在本例中,必须在操场的资源目录中。(注意:您可能需要打开navigator窗口以查看您的游乐场的目录结构。)

import Foundation

if let fileURL = Bundle.main.url(forResource:"MyText", withExtension: "txt")
{
    do {
        let contents = try String(contentsOf: fileURL, encoding: String.Encoding.utf8)
        print(contents)
    } catch {
        print("Error: \(error.localizedDescription)")
    }
} else {
    print("No such file URL.")
}

#5


7  

1. Access a file that is located in the Resources folder of your Playground

With Swift 3, Bundle has a method called url(forResource:withExtension:). url(forResource:withExtension:) has the following declaration:

使用Swift 3, Bundle有一个名为url(forResource:withExtension:)的方法。url(forResource:withExtension:)有以下声明:

func url(forResource name: String?, withExtension ext: String?) -> URL?

Returns the file URL for the resource identified by the specified name and file extension.

返回由指定名称和文件扩展名标识的资源的文件URL。


You can use url(forResource:withExtension:) in order to read the content of a json file located in the Resources folder of an iOS or Mac Playground:

您可以使用url(forResource:withExtension:)读取位于iOS或Mac游乐场资源文件夹中的json文件的内容:

import Foundation

do {
    guard let fileUrl = Bundle.main.url(forResource: "Data", withExtension: "json") else { fatalError() }
    let data = try Data(contentsOf: fileUrl)
    let json = try JSONSerialization.jsonObject(with: data, options: [])
    print(json)
} catch {
    print(error)
}    

You can use url(forResource:withExtension:) in order to read the content of a text file located in the Resources folder of an iOS or Mac Playground:

您可以使用url(forResource:withExtension:)来读取位于iOS或Mac游乐场资源文件夹中的文本文件的内容:

import Foundation

do {
    guard let fileUrl = Bundle.main.url(forResource: "Text", withExtension: "txt") else { fatalError() }
    let text = try String(contentsOf: fileUrl, encoding: String.Encoding.utf8)
    print(text)
} catch {
    print(error)
}

As an alternative to let image = UIImage(named: "image"), you can use url(forResource:withExtension:) in order to access an image located in the Resources folder of an iOS Playground:

作为让image = UIImage(命名为:"image")的替代方法,您可以使用url(forResource:withExtension:)来访问位于iOS游乐场资源文件夹中的图像:

import UIKit

do {
    guard let fileUrl = Bundle.main.url(forResource: "Image", withExtension: "png") else { fatalError() }
    let data = try Data(contentsOf: fileUrl)
    let image = UIImage(data: data)
} catch {
    print(error)
}

2. Access a file that is located in the ~/Documents/Shared Playground Data folder of your computer

With Swift 3, PlaygroundSupport module provides a global constant called playgroundSharedDataDirectory. playgroundSharedDataDirectory has the following declaration:

使用Swift 3, PlaygroundSupport模块提供了一个名为playgroundSharedDataDirectory的全局常量。playgroundSharedDataDirectory有以下声明:

let playgroundSharedDataDirectory: URL

The path to the directory containing data shared between all playgrounds.

包含在所有操场之间共享的数据的目录的路径。


You can use playgroundSharedDataDirectory in order to read the content of a json file located in the ~/Documents/Shared Playground Data folder of your computer from an iOS or Mac Playground:

您可以使用playgroundSharedDataDirectory从iOS或Mac游乐场读取位于您的计算机的~/Documents/Shared游乐场数据文件夹中的json文件的内容:

import Foundation
import PlaygroundSupport

do {
    let fileUrl = PlaygroundSupport.playgroundSharedDataDirectory.appendingPathComponent("Data.json")        
    let data = try Data(contentsOf: fileUrl)
    let json = try JSONSerialization.jsonObject(with: data, options: [])
    print(json)
} catch {
    print(error)
}

You can use playgroundSharedDataDirectory in order to read the content of a text file located in the ~/Documents/Shared Playground Data folder of your computer from an iOS or Mac Playground:

您可以使用playgroundSharedDataDirectory来读取位于您的计算机的~/文档/共享操场数据文件夹中的文本文件的内容,从一个iOS或Mac的游乐场:

import Foundation
import PlaygroundSupport

do {
    let fileUrl = PlaygroundSupport.playgroundSharedDataDirectory.appendingPathComponent("Text.txt")
    let text = try String(contentsOf: fileUrl, encoding: String.Encoding.utf8)
    print(text)
} catch {
    print(error)
}

You can use playgroundSharedDataDirectory in order to access an image located in the ~/Documents/Shared Playground Data folder of your computer from an iOS Playground:

您可以使用playgroundSharedDataDirectory,以便从iOS游乐场访问位于您的计算机的~/Documents/Shared游乐场数据文件夹中的映像:

import UIKit
import PlaygroundSupport

do {
    let fileUrl = PlaygroundSupport.playgroundSharedDataDirectory.appendingPathComponent("Image.png")
    let data = try Data(contentsOf: fileUrl)
    let image = UIImage(data: data)
} catch {
    print(error)
}

#6


2  

  1. Select the .playground file.

    选择.playground文件。

  2. Open Utility inspector, In the playground press opt-cmd-1 to open the File Inspector. You should see the playground on the right. If you don't have it selected, press cmd-1 to open the Project Navigator and click on the playground file.

    打开工具检查器,在操场上按opt-cmd-1打开文件检查器。你应该看看右边的操场。如果您没有选择,请按cmd-1来打开项目导航器并单击操场文件。

  3. Under 'Resource Path' in Playground Settings choose 'Relative To Playground' and platform as OSX.

    在“资源路径”下的游乐场设置,选择“相对于游乐场”和平台作为OSX。

#7


1  

On Mavericks with Xcode 6.0.1 you can read using iOS platform too.

在拥有Xcode 6.0.1的Mavericks中,你也可以使用iOS平台来阅读。

import UIKit
let dirs : [String]? =    NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) as? [String]
let myDir = "/Shared Playground Data"

let file = "README.md" // My change to your code - yours is presumably set off-screen
if (dirs != nil) {
  let directories:[String] = dirs!;
  let dir = directories[0] + myDir; // iOS playground documents directory
  let path = dir.stringByAppendingPathComponent(file);

  //read
  let content = String.stringWithContentsOfFile(path, encoding: NSUTF8StringEncoding, error: nil)
  // works...
  println(content!)
}

Remember, you need to create a directory called "Shared Playground Data" in your Documents directory. Im my case I used this command: mkdir "/Users/joao_parana/Documents/Shared Playground Data" and put there my file README.md

请记住,您需要在文档目录中创建一个名为“共享游乐场数据”的目录。我使用了这个命令:mkdir "/Users/joao_parana/Documents/Shared Playground Data"并将我的file README.md放在那里

#8


1  

String.stringWithContentsOfFile is DEPRECATED and doesn't work anymore with Xcode 6.1.1

字符串。stringWithContentsOfFile被弃用,不再使用Xcode 6.1.1。

Create your documentDirectoryUrl

创建您的documentDirectoryUrl

let documentDirectoryUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! as NSURL

To make sure the file is located there you can use the finder command Go To Folder e copy paste the printed documentDirectoryUrl.path there

要确保文件位于那里,您可以使用查找器命令到文件夹e复制粘贴打印的documentDirectoryUrl。路径有

println(documentDirectoryUrl.path!)
// should look like this: /Users/userName/Library/Containers/com.apple.dt.playground.stub.OSX.PLAYGROUNDFILENAME-5AF5B25D-D0D1-4B51-A297-00015EE97F13/Data/Documents

Just append the file name to the folder url as a path component

只需将文件名作为路径组件附加到文件夹url

let fileNameUrl = documentDirectoryUrl.URLByAppendingPathComponent("ReadMe.txt")
var fileOpenError:NSError?

Check if the file exists before attempting to open it

在尝试打开文件之前,检查文件是否存在

if NSFileManager.defaultManager().fileExistsAtPath(fileNameUrl.path!) {

    if let fileContent = String(contentsOfURL: fileNameUrl, encoding: NSUTF8StringEncoding, error: &fileOpenError) {
        println(fileContent)        // prints ReadMe.txt contents if successful
    } else {
        if let fileOpenError = fileOpenError {
            println(fileOpenError)  // Error Domain=NSCocoaErrorDomain Code=XXX "The file “ReadMe.txt” couldn’t be opened because...."
        }
    }
} else {
    println("file not found")
}

#1


4  

This works for me. The only thing I changed was to be explicit about the file name (which is implied in your example) - perhaps you have a typo in the off-screen definition of the "file" variable?

这适合我。我唯一更改的是文件名称的显式(在您的示例中包含)——也许您在“file”变量的屏幕外定义中有一个错误?

let dirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) as? [String]

let file = "trial.txt" // My change to your code - yours is presumably set off-screen
if let directories = dirs {
  let dir = directories[0]; //documents directory
  let path = dir.stringByAppendingPathComponent(file);

  //read
  let content = NSString(contentsOfFile: path, usedEncoding: nil, error: nil)
  // works...
}

#2


49  

You can also put your file into your playground's resources. To do this: show Project Navigator with CMD + 1. Drag and drop your file into the resources folder. Then read the file:

你也可以把你的档案放在你的操场的资源。为此:使用CMD + 1显示项目导航器。将您的文件拖放到资源文件夹中。然后读取文件:

On XCode 6.4 and Swift 1.2:

关于x6.4和Swift 1.2:

var error: NSError?
let fileURL = NSBundle.mainBundle().URLForResource("Input", withExtension: "txt")
let content = String(contentsOfURL: fileURL!, encoding: NSUTF8StringEncoding, error: &error)

On XCode 7 and Swift 2:

关于XCode 7和Swift 2:

let fileURL = NSBundle.mainBundle().URLForResource("Input", withExtension: "txt")
let content = try String(contentsOfURL: fileURL!, encoding: NSUTF8StringEncoding)

On XCode 8 and Swift 3:

关于XCode 8和Swift 3:

let fileURL = Bundle.main.url(forResource: "Input", withExtension: "txt")
let content = try String(contentsOf: fileURL!, encoding: String.Encoding.utf8)

If the file has binary data, you can use NSData(contentsOfURL: fileURL!) or Data(contentsOf: fileURL!) (for Swift 3).

如果文件有二进制数据,您可以使用NSData(contentsOfURL: fileURL!)或data (contentsOf: fileURL!)(用于Swift 3)。

#3


26  

While the answer has been supplied for a quick fix, there is a better solution.

虽然已经提供了快速修复的答案,但还有更好的解决方案。

Each time the playground is opened it will be assigned a new container. This means using the normal directory structure you would have to copy the file you want into the new container every time.

每次打开操场,就会给它分配一个新的容器。这意味着要使用正常的目录结构,每次都要将您想要的文件复制到新容器中。

Instead, inside the container there is a symbolic link to a Shared Playground Data directory (/Users//Documents/Shared Playground Data) which remains when reopening the playground, and can be accessed from multiple playgrounds.

相反,在容器内部有一个符号链接,链接到共享的操场数据目录(/用户//文档/共享的操场数据),在重新打开操场时,可以从多个操场访问。

You can use XCPlayground to access this shared folder.

您可以使用xc游乐场来访问这个共享文件夹。

import XCPlayground

let path = XCPlaygroundSharedDataDirectoryURL.appendingPathComponent("foo.txt")

The official documentation can be found here: XCPlayground Module Reference

官方文档可以在这里找到:xc游乐场模块参考

Cool post on how to organize this directory per-playground: Swift, Playgrounds, and XCPlayground

关于如何组织这个目录的酷帖子:斯威夫特,操场,和xc操场。

#4


10  

Swift 3 (Xcode 8)

斯威夫特3(Xcode 8)

The code below works in both iOS and macOS playgrounds. The text file ("MyText.txt" in this example) must be in the Resources directory of the playground. (Note: You may need to open the navigator window to see the directory structure of your playground.)

下面的代码可以在iOS和macOS上运行。文本文件(“MyText。在本例中,必须在操场的资源目录中。(注意:您可能需要打开navigator窗口以查看您的游乐场的目录结构。)

import Foundation

if let fileURL = Bundle.main.url(forResource:"MyText", withExtension: "txt")
{
    do {
        let contents = try String(contentsOf: fileURL, encoding: String.Encoding.utf8)
        print(contents)
    } catch {
        print("Error: \(error.localizedDescription)")
    }
} else {
    print("No such file URL.")
}

#5


7  

1. Access a file that is located in the Resources folder of your Playground

With Swift 3, Bundle has a method called url(forResource:withExtension:). url(forResource:withExtension:) has the following declaration:

使用Swift 3, Bundle有一个名为url(forResource:withExtension:)的方法。url(forResource:withExtension:)有以下声明:

func url(forResource name: String?, withExtension ext: String?) -> URL?

Returns the file URL for the resource identified by the specified name and file extension.

返回由指定名称和文件扩展名标识的资源的文件URL。


You can use url(forResource:withExtension:) in order to read the content of a json file located in the Resources folder of an iOS or Mac Playground:

您可以使用url(forResource:withExtension:)读取位于iOS或Mac游乐场资源文件夹中的json文件的内容:

import Foundation

do {
    guard let fileUrl = Bundle.main.url(forResource: "Data", withExtension: "json") else { fatalError() }
    let data = try Data(contentsOf: fileUrl)
    let json = try JSONSerialization.jsonObject(with: data, options: [])
    print(json)
} catch {
    print(error)
}    

You can use url(forResource:withExtension:) in order to read the content of a text file located in the Resources folder of an iOS or Mac Playground:

您可以使用url(forResource:withExtension:)来读取位于iOS或Mac游乐场资源文件夹中的文本文件的内容:

import Foundation

do {
    guard let fileUrl = Bundle.main.url(forResource: "Text", withExtension: "txt") else { fatalError() }
    let text = try String(contentsOf: fileUrl, encoding: String.Encoding.utf8)
    print(text)
} catch {
    print(error)
}

As an alternative to let image = UIImage(named: "image"), you can use url(forResource:withExtension:) in order to access an image located in the Resources folder of an iOS Playground:

作为让image = UIImage(命名为:"image")的替代方法,您可以使用url(forResource:withExtension:)来访问位于iOS游乐场资源文件夹中的图像:

import UIKit

do {
    guard let fileUrl = Bundle.main.url(forResource: "Image", withExtension: "png") else { fatalError() }
    let data = try Data(contentsOf: fileUrl)
    let image = UIImage(data: data)
} catch {
    print(error)
}

2. Access a file that is located in the ~/Documents/Shared Playground Data folder of your computer

With Swift 3, PlaygroundSupport module provides a global constant called playgroundSharedDataDirectory. playgroundSharedDataDirectory has the following declaration:

使用Swift 3, PlaygroundSupport模块提供了一个名为playgroundSharedDataDirectory的全局常量。playgroundSharedDataDirectory有以下声明:

let playgroundSharedDataDirectory: URL

The path to the directory containing data shared between all playgrounds.

包含在所有操场之间共享的数据的目录的路径。


You can use playgroundSharedDataDirectory in order to read the content of a json file located in the ~/Documents/Shared Playground Data folder of your computer from an iOS or Mac Playground:

您可以使用playgroundSharedDataDirectory从iOS或Mac游乐场读取位于您的计算机的~/Documents/Shared游乐场数据文件夹中的json文件的内容:

import Foundation
import PlaygroundSupport

do {
    let fileUrl = PlaygroundSupport.playgroundSharedDataDirectory.appendingPathComponent("Data.json")        
    let data = try Data(contentsOf: fileUrl)
    let json = try JSONSerialization.jsonObject(with: data, options: [])
    print(json)
} catch {
    print(error)
}

You can use playgroundSharedDataDirectory in order to read the content of a text file located in the ~/Documents/Shared Playground Data folder of your computer from an iOS or Mac Playground:

您可以使用playgroundSharedDataDirectory来读取位于您的计算机的~/文档/共享操场数据文件夹中的文本文件的内容,从一个iOS或Mac的游乐场:

import Foundation
import PlaygroundSupport

do {
    let fileUrl = PlaygroundSupport.playgroundSharedDataDirectory.appendingPathComponent("Text.txt")
    let text = try String(contentsOf: fileUrl, encoding: String.Encoding.utf8)
    print(text)
} catch {
    print(error)
}

You can use playgroundSharedDataDirectory in order to access an image located in the ~/Documents/Shared Playground Data folder of your computer from an iOS Playground:

您可以使用playgroundSharedDataDirectory,以便从iOS游乐场访问位于您的计算机的~/Documents/Shared游乐场数据文件夹中的映像:

import UIKit
import PlaygroundSupport

do {
    let fileUrl = PlaygroundSupport.playgroundSharedDataDirectory.appendingPathComponent("Image.png")
    let data = try Data(contentsOf: fileUrl)
    let image = UIImage(data: data)
} catch {
    print(error)
}

#6


2  

  1. Select the .playground file.

    选择.playground文件。

  2. Open Utility inspector, In the playground press opt-cmd-1 to open the File Inspector. You should see the playground on the right. If you don't have it selected, press cmd-1 to open the Project Navigator and click on the playground file.

    打开工具检查器,在操场上按opt-cmd-1打开文件检查器。你应该看看右边的操场。如果您没有选择,请按cmd-1来打开项目导航器并单击操场文件。

  3. Under 'Resource Path' in Playground Settings choose 'Relative To Playground' and platform as OSX.

    在“资源路径”下的游乐场设置,选择“相对于游乐场”和平台作为OSX。

#7


1  

On Mavericks with Xcode 6.0.1 you can read using iOS platform too.

在拥有Xcode 6.0.1的Mavericks中,你也可以使用iOS平台来阅读。

import UIKit
let dirs : [String]? =    NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) as? [String]
let myDir = "/Shared Playground Data"

let file = "README.md" // My change to your code - yours is presumably set off-screen
if (dirs != nil) {
  let directories:[String] = dirs!;
  let dir = directories[0] + myDir; // iOS playground documents directory
  let path = dir.stringByAppendingPathComponent(file);

  //read
  let content = String.stringWithContentsOfFile(path, encoding: NSUTF8StringEncoding, error: nil)
  // works...
  println(content!)
}

Remember, you need to create a directory called "Shared Playground Data" in your Documents directory. Im my case I used this command: mkdir "/Users/joao_parana/Documents/Shared Playground Data" and put there my file README.md

请记住,您需要在文档目录中创建一个名为“共享游乐场数据”的目录。我使用了这个命令:mkdir "/Users/joao_parana/Documents/Shared Playground Data"并将我的file README.md放在那里

#8


1  

String.stringWithContentsOfFile is DEPRECATED and doesn't work anymore with Xcode 6.1.1

字符串。stringWithContentsOfFile被弃用,不再使用Xcode 6.1.1。

Create your documentDirectoryUrl

创建您的documentDirectoryUrl

let documentDirectoryUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! as NSURL

To make sure the file is located there you can use the finder command Go To Folder e copy paste the printed documentDirectoryUrl.path there

要确保文件位于那里,您可以使用查找器命令到文件夹e复制粘贴打印的documentDirectoryUrl。路径有

println(documentDirectoryUrl.path!)
// should look like this: /Users/userName/Library/Containers/com.apple.dt.playground.stub.OSX.PLAYGROUNDFILENAME-5AF5B25D-D0D1-4B51-A297-00015EE97F13/Data/Documents

Just append the file name to the folder url as a path component

只需将文件名作为路径组件附加到文件夹url

let fileNameUrl = documentDirectoryUrl.URLByAppendingPathComponent("ReadMe.txt")
var fileOpenError:NSError?

Check if the file exists before attempting to open it

在尝试打开文件之前,检查文件是否存在

if NSFileManager.defaultManager().fileExistsAtPath(fileNameUrl.path!) {

    if let fileContent = String(contentsOfURL: fileNameUrl, encoding: NSUTF8StringEncoding, error: &fileOpenError) {
        println(fileContent)        // prints ReadMe.txt contents if successful
    } else {
        if let fileOpenError = fileOpenError {
            println(fileOpenError)  // Error Domain=NSCocoaErrorDomain Code=XXX "The file “ReadMe.txt” couldn’t be opened because...."
        }
    }
} else {
    println("file not found")
}