从 swift 中的文本文件加载单独的组件
Loading separate components from textfile in swift
我正在尝试从一个文本文件中分配四个不同的变量。
但问题是 - 它只是作为一个片段加载到每个变量中。
这是我的代码
class QuestionMark
{
var Question = text.componentsSeparatedByString(">>")
var Contents = text.componentsSeparatedByString("--")
var option1 = text.componentsSeparatedByString("[")
var option2 = text.componentsSeparatedByString("]")
init(Question: [String], Contents: [String], option1: [String], option2: [String])
{
self.Question = Question
self.Contents = Contents
self.option1 = option1
self.option2 = option2
}
这是我正在使用的文本文件
>>Here is the grocery question
--Apples
--Oranges
[pickApples]pickOranges
我可能做错了什么?
感谢任何见解!
遗憾的是,Swift 并没有让字符串解析变得更舒服。这是让您入门的东西。下面的代码将在换行符上拆分输入字符串,遍历结果并根据每行的前缀将不同的组件读入可选值。
let text = ">>Here is the grocery question\n--Apples\n--Oranges\n[pickApples]pickOranges"
let lines = split(text) { [=10=] == "\n" }
var question: String?
var contents: [String]?
var option1: String?
var option2: String?
for line in lines {
if line.hasPrefix(">>") {
question = line.substringFromIndex(advance(line.startIndex, 2))
} else if line.hasPrefix("--") {
if contents == nil {
contents = [String]()
}
contents?.append(line.substringFromIndex(advance(line.startIndex, 2)))
} else if line.hasPrefix("[") {
if let index = line.rangeOfString("]")?.startIndex {
option1 = line.substringWithRange(Range<String.Index>(
start: advance(line.startIndex, 1), end: index))
option2 = line.substringWithRange(Range<String.Index>(
start: advance(index, 1), end: line.endIndex))
}
}
}
println(question!)
println(contents!)
println(option1!)
println(option2!)
上面的代码片段将被打印出来
Here is the grocery question
[Apples, Oranges]
pickApples
pickOranges
我正在尝试从一个文本文件中分配四个不同的变量。 但问题是 - 它只是作为一个片段加载到每个变量中。
这是我的代码
class QuestionMark
{
var Question = text.componentsSeparatedByString(">>")
var Contents = text.componentsSeparatedByString("--")
var option1 = text.componentsSeparatedByString("[")
var option2 = text.componentsSeparatedByString("]")
init(Question: [String], Contents: [String], option1: [String], option2: [String])
{
self.Question = Question
self.Contents = Contents
self.option1 = option1
self.option2 = option2
}
这是我正在使用的文本文件
>>Here is the grocery question
--Apples
--Oranges
[pickApples]pickOranges
我可能做错了什么?
感谢任何见解!
遗憾的是,Swift 并没有让字符串解析变得更舒服。这是让您入门的东西。下面的代码将在换行符上拆分输入字符串,遍历结果并根据每行的前缀将不同的组件读入可选值。
let text = ">>Here is the grocery question\n--Apples\n--Oranges\n[pickApples]pickOranges"
let lines = split(text) { [=10=] == "\n" }
var question: String?
var contents: [String]?
var option1: String?
var option2: String?
for line in lines {
if line.hasPrefix(">>") {
question = line.substringFromIndex(advance(line.startIndex, 2))
} else if line.hasPrefix("--") {
if contents == nil {
contents = [String]()
}
contents?.append(line.substringFromIndex(advance(line.startIndex, 2)))
} else if line.hasPrefix("[") {
if let index = line.rangeOfString("]")?.startIndex {
option1 = line.substringWithRange(Range<String.Index>(
start: advance(line.startIndex, 1), end: index))
option2 = line.substringWithRange(Range<String.Index>(
start: advance(index, 1), end: line.endIndex))
}
}
}
println(question!)
println(contents!)
println(option1!)
println(option2!)
上面的代码片段将被打印出来
Here is the grocery question
[Apples, Oranges]
pickApples
pickOranges