How can I read a file in a swift playground

后端 未结 9 1855
时光说笑
时光说笑 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: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)
    }
    

提交回复
热议问题