简体   繁体   中英

Swift Array, TextFile?

I have a tableview that displays the following navigation from one tableview to another:

  • Chapter 1
    • Topic 1
      • Heading 1
        • Sub Heading 1
          • Content 1

This is done using the following code:

 if var path = NSBundle.mainBundle().pathForResource("Chapters", ofType: "txt"){
         var data = String(contentsOfFile:path, encoding: NSUTF8StringEncoding, error: nil)
         if var content = (data){

             //Breaks the entire String into individual strings at each newLine
             var line: [String] = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())

             //First Table - Displays Chapters
             var lineArray = [line[0],
                              line[6],
                              line[12]]

How can I get it to read this instead?

Topics: Food, color, animals, cars //each as a separate string. If I use this, it will display the whole line as a text starting from "Topics: ...." so basically what I'm asking is, is there a way to read past Topics, and just Start the first string[0] at Food?

I don't know I'm understand your question or not but I think you want to skip topics: from string Topics: Food, Water, Melon, Car ....So after separate the string you want Food at index 0...

  var str = "Topics: Food, color, animals, cars"

  // Get location of first Space or any character you want
  let loc = find(str, " ")

  //Get the string from first space to end of string     
  let skipstr = Range(start: loc!,
            end: str.endIndex)
  let finalStr = str.substringWithRange(skipstr)

  println(finalStr)  
  //Output:-->  Food, color, animals, cars

And then just separate string from comma

 let indexes = finalStr.componentsSeparatedByString(",")
 println(indexes[0])
 //And you got Food at index 0

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