2016-02-04 101 views
0

我想使用Swift 2.0解析一個.csv文件(和輸出)到Xcode中的UITextView。有麻煩找出正確的方式我parseCSV()函數調用輸出解析的.csv到我的UITextView解析一個.csv文件到UITextView時出錯

代碼:

// Parsing Function 
func parseCSV (contentsOfURL: NSURL, encoding: NSStringEncoding, error: NSErrorPointer) -> [(id:String, name:String, age: String)]? { 
    // Load the CSV file and parse it 
    let delimiter = "," 
    var peoples:[(id: String, name: String, age: String)]? 

    if let data = NSData(contentsOfURL: contentsOfURL){ 
     if let content = NSString(data: data, encoding: NSUTF8StringEncoding) { 
      peoples = [] 
      let lines:[String] = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) as [String] 

      for line in lines { 
       var values:[String] = [] 
       if line != "" { 
        // For a line with double quotes 
        // we use NSScanner to perform the parsing 
        if line.rangeOfString("\"") != nil { 
         var textToScan:String = line 
         var value:NSString? 
         var textScanner:NSScanner = NSScanner(string: textToScan) 
         while textScanner.string != "" { 

          if (textScanner.string as NSString).substringToIndex(1) == "\"" { 
           textScanner.scanLocation += 1 
           textScanner.scanUpToString("\"", intoString: &value) 
           textScanner.scanLocation += 1 
          } else { 
           textScanner.scanUpToString(delimiter, intoString: &value) 
          } 

          // Store the value into the values array 
          values.append(value as! String) 

          // Retrieve the unscanned remainder of the string 
          if textScanner.scanLocation < textScanner.string.characters.count { 
           textToScan = (textScanner.string as NSString).substringFromIndex(textScanner.scanLocation + 1) 
          } else { 
           textToScan = "" 
          } 
          textScanner = NSScanner(string: textToScan) 
         } 

         // For a line without double quotes, we can simply separate the string 
         // by using the delimiter (e.g. comma) 
        } else { 
         values = line.componentsSeparatedByString(delimiter) 
        } 

        // Put the values into the tuple and add it to the items array 
        let people = (id: values[0], name: values[1], age: values[2]) 
        peoples?.append(people) 
       } 

      } 
     } 
    } 

    return peoples 
} 

後來我的viewDidLoad()函數如下:

@IBOutlet weak var parseText: UITextView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let url = NSURL(string: "https://www.dropbox.com/home/CSV?preview=test.csv") 
    let error = NSErrorPointer() 
    parseText.text = parseCSV(url!, encoding: NSUTF8StringEncoding, error: error) 
} 

我得到.text是String類型的! (因此導致一個錯誤),但我不知道要在parseText上調用以確保正確的類型[(id:String, name:String, age: String)]

錯誤: http://imgur.com/EYB1Z27

乾杯

回答

0

嘗試以下

 func parseCSV (contentsOfURL: NSURL,encoding: NSStringEncoding, error: NSErrorPointer) 
    [(name:String, detail:String, price: String)] 

    // Load the CSV file and parse it 

    let delimiter = "," 
    var items: [(name:String, detail:String, price: String)]? 

    if let data = NSData(contentsOfURL: contentsOfURL) { 

    if let content = NSString(data: data, encoding: NSUTF8StringEncoding) { 
     items = [] 
     let lines:[String] = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) as [String] 

     for line in lines { 
      var values:[String] = [] 
      if line != "" { 
       // For a line with double quotes 
       // we use NSScanner to perform the parsing 
       if line.rangeOfString("\"") != nil { 
        var textToScan:String = line 
        var value:NSString? 
        var textScanner:NSScanner = NSScanner(string: textToScan) 
        while textScanner.string != "" { 
         if (textScanner.string as NSString).substringToIndex(1) == "\"" { 
          textScanner.scanLocation += 1 
          textScanner.scanUpToString("\"", intoString: &value) 
          textScanner.scanLocation += 1 
         } else { 
          textScanner.scanUpToString(delimiter, intoString: &value) 
         } 

         // Store the value into the values array 
         values.append(value as! String) 

         // Retrieve the unscanned remainder of the string 


         if textScanner.scanLocation < textScanner.string.characters.count { 
          textToScan = (textScanner.string as NSString).substringFromIndex(textScanner.scanLocation + 1) 
         } else { 
          textToScan = "" 
         } 
         textScanner = NSScanner(string: textToScan) 
        } 

         // For a line without double quotes, we can simply separate the string 
        // by using the delimiter (e.g. comma) 
       } else { 
        values = line.componentsSeparatedByString(delimiter) 
       } 

       // Put the values into the tuple and add it to the items array 
       let item = (name: values[0], detail: values[1], price: values[2]) 
       items?.append(item) 
      } 
     } 
    } 
    } 
    return items 

}