iosarraysswiftuitableviewtextreader

Swift Array, TextFile?


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

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?


Solution

  • 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