How can I read a file in a swift playground

后端 未结 9 1835
时光说笑
时光说笑 2020-12-12 22:37

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

let dirs : String[]? =    NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory         


        
相关标签:
9条回答
  • 2020-12-12 23:01
    1. Select the .playground file.

    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.

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

    0 讨论(0)
  • 2020-12-12 23:02

    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:

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

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

    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:

    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:

    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:

    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:

    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:

    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:

    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:

    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)
    }
    
    0 讨论(0)
  • 2020-12-12 23:05

    Swift 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.)

    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.")
    }
    
    0 讨论(0)
提交回复
热议问题