How do I load a text file line by line into an array with Swift?

前端 未结 9 715
盖世英雄少女心
盖世英雄少女心 2020-12-29 18:51

How do I load a text file line by line into an array with swift?

相关标签:
9条回答
  • 2020-12-29 19:28

    For me works as follow:

    let myFileURL = NSBundle.mainBundle().URLForResource("listacomuni", withExtension: "txt")!
    let myText = try! String(contentsOfURL: myFileURL, encoding: NSISOLatin1StringEncoding)
    print(String(myText))
    
    0 讨论(0)
  • 2020-12-29 19:32

    My simple coding for you

     let path = NSBundle.mainBundle().pathForResource("FileName", ofType: "txt")
     var text = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)!
     println(text)
     var array = text.componentsSeparatedByString("\n")
    
    0 讨论(0)
  • 2020-12-29 19:33

    Swift 3 version based on the accepted answer:

    func arrayFromContentsOfFileWithName(fileName: String) -> [String]? {
        guard let path = Bundle.main.path(forResource: fileName, ofType: "txt") else {
            return nil
        }
    
        do {
            let content = try String(contentsOfFile:path, encoding: String.Encoding.utf8)
            return content.components(separatedBy: "\n")
        } catch {
            return nil
        }
    }
    
    0 讨论(0)
  • 2020-12-29 19:39

    If you want to read a csv file of numeric data. (based on Cezar's answer)

    func get_csv_data() -> [[Double]] {
        guard let path = NSBundle.mainBundle().pathForResource("filename_without_extension", ofType: "csv") else {
            return []
        }
        do {
            let content = try String(contentsOfFile:path, encoding: NSUTF8StringEncoding)
            let line_str_array = content.componentsSeparatedByString("\n")
            return line_str_array.map {
                let field_str_array = $0.componentsSeparatedByString(",")
                return field_str_array.map {
                    Double($0)!
                }
            }
        } catch _ as NSError {
            return []
        }
    }
    
    0 讨论(0)
  • 2020-12-29 19:40

    With Swift 5, according to your needs, you can choose one of the 3 following ways in order to solve your problem.


    #1. Using StringProtocol's components(separatedBy:) method

    Foundation provides String a method called components(separatedBy:) with the following declaration:

    func components(separatedBy separator: CharacterSet) -> [String]
    

    Returns an array containing substrings from the string that have been divided by characters in the given set.

    The code sample below shows how to use components(separatedBy:) with its parameter set to CharacterSet.newlines in order to load the content of a text file line by line into an array:

    import Foundation
    
    let path = Bundle.main.path(forResource: "Lorem Ipsum", ofType: "txt")!
    let text = try! String(contentsOfFile: path, encoding: String.Encoding.utf8)
    let lines = text.components(separatedBy: CharacterSet.newlines)
    
    print(lines)
    

    As an alternative, you can use the overloading of components(separatedBy:) that takes a parameter of type String. The code sample below shows how to use it:

    import Foundation
    
    let path = Bundle.main.path(forResource: "Lorem Ipsum", ofType: "txt")!
    let text = try! String(contentsOfFile: path, encoding: String.Encoding.utf8)
    let lines = text.components(separatedBy: "\n")
    
    print(lines)
    

    ⚠️ You should however prefer the overloading of components(separatedBy:) that takes a CharacterSet parameter and use it with the value CharacterSet.newlines as this will manage all new line characters (U+000A ~ U+000D, U+0085, U+2028, and U+2029).


    #2. Using StringProtocol's enumerateSubstrings(in:options:_:) method

    Foundation provides String a method called enumerateSubstrings(in:options:_:). The code sample below shows how to use enumerateSubstrings(in:options:_:) with options parameter value set to String.EnumerationOptions.byLines in order to load the content of a text file line by line into an array:

    import Foundation
    
    let path = Bundle.main.path(forResource: "Lorem Ipsum", ofType: "txt")!
    let text = try! String(contentsOfFile: path, encoding: String.Encoding.utf8)
    
    let range = text.startIndex ..< text.endIndex
    var lines = [String]()
    text.enumerateSubstrings(in: range, options: String.EnumerationOptions.byLines) {
        (substring, range, enclosingRange, stop) in
        guard let substring = substring else { return }
        lines.append(substring)
    }
    
    print(lines)
    

    #3. Using NLTokenizer's enumerateTokens(in:using:) method

    NLTokenizer has a method called enumerateTokens(in:using:). enumerateTokens(in:using:) has the following declaration:

    @nonobjc func enumerateTokens(in range: Range<String.Index>, using block: (Range<String.Index>, NLTokenizer.Attributes) -> Bool)
    

    Enumerates over a given range of the string and calls the specified block for each token.

    The code sample below shows how to use enumerateTokens(in:using:) in order to load the content of a text file line by line into an array:

    import Foundation
    import NaturalLanguage
    
    let path = Bundle.main.path(forResource: "Lorem Ipsum", ofType: "txt")!
    let text = try! String(contentsOfFile: path, encoding: String.Encoding.utf8)
    
    let tokenizer = NLTokenizer(unit: .paragraph)
    tokenizer.setLanguage(.english)
    tokenizer.string = text
    
    var lines = [String]()
    tokenizer.enumerateTokens(in: text.startIndex ..< text.endIndex) { (range, attributes) -> Bool in
        let line = String(text[range])
        lines.append(line)
        return true
    }
    print(lines)
    
    0 讨论(0)
  • 2020-12-29 19:44

    Swift 3.0

    if let path = Bundle.main.path(forResource: <#FileName#>, ofType: "txt")
    {
        do
        {
            let str = try String(contentsOfFile:path, encoding: String.Encoding.utf8)
            return str.components(separatedBy: "\n")
        }
        catch
        {
    
        }
    }
    else
    {
        return nil
    }
    
    0 讨论(0)
提交回复
热议问题