几个变量的字符串插值导致表达式太复杂而无法解决

时间:2015-01-12 13:01:38

标签: swift

我有一组变量并形成一个基于xml的字符串,其中包含这些变量的值,但它导致表达式太复杂而无法在合理的时间内解决,而且它建议在块中分解语句然后我会丢失可读性。有没有更好的方法以这种方式形成大字符串插值。输出字符串将转到A XML解析器进行遍历。

var person1 = (first: "John", last: "Doe")
var person2 = (first: "Jane", last: "Doe")
var person3 = (first: "Jane", last: "Roe")
var person4 = (first: "Jonnie", last: "Doe")
var person5 = (first: "Janie", last: "Doe")


var persons =
"<persons>" +
    "<person first='\(person1.first)' last='\(person1.last)' />" +
    "<person first='\(person2.first)' last='\(person2.last)' />" +
    "<person first='\(person3.first)' last='\(person3.last)' />" +
    "<person first='\(person4.first)' last='\(person4.last)' />" +
    "<person first='\(person5.first)' last='\(person5.last)' />" +
"</persons>"

修改 可能存在结构嵌套,为简洁起见,我创建了一个简约的例子,但循环不起作用。

编辑2 - 我可以解决的可能解决方案是将它们存储在字典中

您可以将其视为硬编码值,但希望在手动编码时带来结构和清洁度。

var persons = [ //in this way structures can be nested and seems more elegant
    "<persons>",
        "<person first='\(person1.first)' last='\(person1.last)' />",
        "<person first='\(person2.first)' last='\(person2.last)' />",
    "</persons>"
]

5 个答案:

答案 0 :(得分:1)

其他人已经为您提供了一些很好的解决方案,但是如果您可以将数据转换为数组形式(或者任何序列类型都可以),那么可以尝试其他方法:

let personsArray = [
    (first: "John", last: "Doe"),
    (first: "Jane", last: "Doe"),
    (first: "Jane", last: "Roe"),
    (first: "Jonnie", last: "Doe"),
    (first: "Janie", last: "Doe"),
]

let personToEntry = { 
  (first: String, last: String) in
    "<person first='\(first)' last='\(last)' />"
}

let personEntries = lazy(personsArray).map(personToEntry)

let persons =
"<persons>" + "\n\t" +
    "\n\t".join(personEntries) + "\n" +
"</persons>"

println(persons)

当然,您不必单独声明所有这些内容,您可以将joinmap和封闭所有内容混合到一行。但我发现将它们分开可以更具可读性。

答案 1 :(得分:1)

我知道这已经得到了回答,但是想要在尝试生成一些复杂的XML字符串的过程中挣扎一段时间之后添加我今天发现的内容。

虽然失败了......

let xml =
    "<Start>" + 
    "<tag>\(aVariable)</tag>" + 
    "<tag>\(anotherVariable)</tag>" + //repeated a quite a few more times
    "</Start>"  

我通过专门输入我的变量来解决这个问题。这成功并且编译得相当快

let xml: String =
    "<Start>" + 
    "<tag>\(aVariable)</tag>" + 
    "<tag>\(anotherVariable)</tag>" + //repeated a quite a few more times
    "</Start>" 

答案 2 :(得分:0)

您可以这样做以避免插值

var persons =
"<persons>" +
    "<person first='"+person1.first+"' last='"+person1.last+"' />" +
        "<person first='"+person2.first+"' last='"+person2.last+"' />" +
        "<person first='"+person3.first+"' last='"+person3.last+"' />" +
        "<person first='"+person4.first+"' last='"+person4.last+"' />" +
        "<person first='"+person5.first+"' last='"+person5.last+"' />" +
"</persons>"

答案 3 :(得分:0)

一种方法是使用数组:

    var person1 = (first: "John", last: "Doe")
    var person2 = (first: "Jane", last: "Doe")
    var person3 = (first: "Jane", last: "Roe")
    var person4 = (first: "Jonnie", last: "Doe")
    var person5 = (first: "Janie", last: "Doe")

    var people = [person1,person2,person3,person4]

    var persons = "<persons>"

    for p in people {
        persons += "<person first='\(p.first)' last='\(p.last)' />"
    }
    persons += "</persons>"

另一种方法是使用多个表达式(你可以或多或少地拆分它):

    var personsb = "<persons>"
    personsb += "<person first='\(person1.first)' last='\(person1.last)' />"
    personsb += "<person first='\(person2.first)' last='\(person2.last)' />"
    personsb += "<person first='\(person3.first)' last='\(person3.last)' />"
    personsb += "<person first='\(person4.first)' last='\(person4.last)' />"
    personsb += "<person first='\(person5.first)' last='\(person5.last)' />"
    personsb += "</persons>"

答案 4 :(得分:0)

首先,似乎目前Xcode无法处理非常大的表达式。对于你的问题:我建议你使用一个循环的数组来生成字符串。

let persons : [ (first: String, last: String) ] =  [ ... ]

var personsString = "<persons>"
for (first,last) in persons
{
    personsString += "<person first=" + first + " last=" + last + "/>"
}
personsString += "</persons>"