我正在尝试使用Swift 2.0将.csv文件(和输出)解析为Xcode中的UITextView。无法确定调用我的parseCSV()函数的正确方法,以将解析后的.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)]
的正确类型?
干杯
答案 0 :(得分: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
}