简体   繁体   中英

How can I use a txt file in an Array iOS Swift

I am trying to use data from a txt file in an array to display on different TextViews dependent on the TableCell selected. How can I use the txt file below in an array?

    let path = NSBundle.mainBundle().pathForResource("HelloWorldProgramming", ofType: "txt")

    if let content = String(contentsOfFile:path!, encoding: NSUTF8StringEncoding, error: nil) {

        TextView.text = content
    }

You can separate each line of the text file as a string in an array like so:

let path = NSBundle.mainBundle().pathForResource("HelloWorldProgramming", ofType: "txt")
if let content = String(contentsOfFile:path!, encoding: NSUTF8StringEncoding, error: nil) {
    let arrayOfLines = content.componentsSeparatedByString( "\n" )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM