表达太复杂,无法在合理的时间内解决;考虑将表达式分解为不同的子表达式?

时间:2017-03-23 07:59:00

标签: ios iphone swift swift2

class Lights { 
    // key is id of the light
    // value is the Light object which contains all the attributes
    Map<String, Light> allLights;
}

class Light {
    // all the attributes
    State state;
    String type;
    ...
}

在标题中使用“+”运算符来连接字符串时出现上述错误。 是否有任何方法来解决这个问题,因为我有一个批量数据与“+”运算符连接字符串。 请帮忙

3 个答案:

答案 0 :(得分:0)

将您的字符串拆分为某些字符串,然后更改为Swift 3.0 API

    let a = "Your baby in week three\nThree weeks into your pregnancy, and you might be wondering just how this little miracle occurred (as in the science bit we will assume you know the other part!). \n\n"
    let b = "So here goes: the joining of your partner's sperm and your egg at the moment of conception resulted in a ball of cells (or a blastocyst, if you want to get really technical) which is now starting the 6 day journey from the fallopian tube to the uterus where it will attach itself to the lining of your womb and in nine months time, will result in a real live baby in your arms! \n" +
    "\n"
    let c = "Amazingly, although you will not know for a few months (or until the birth) the <b><font color=#00690c >sex of your baby</font></b> has already been determined. The fertilised egg contains <b><font color=#e3007b>46</font></b> chromosomes with <b><font color=#e3007b>23</font></b> from you and <b><font color=#e3007b>23</font></b> from the dad. You as mum will always provide the <b><font color=#e3007b>X</font></b> chromosome but the dad can provide an <b><font color=#e3007b>X</font></b> or <b><font color=#e3007b>Y</font></b> chromosome. <b><font color=#e3007b>XX</font></b> and you will be having a girl, <b><font color=#e3007b>XY</font></b> and you will have a little boy in 9 months time!"
    let d = a+b+c

    let attrStr = try! NSAttributedString.init(data: d.data(using: String.Encoding.unicode, allowLossyConversion:true)!, options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)

    trimestercontent.attributedText = attrStr

答案 1 :(得分:0)

在调用NSAttributedString

的init方法之前,只需创建文字字符串即可
let string = "Your baby in week three<br>" +
    "Three weeks into your pregnancy, and you might be wondering just how this little miracle occurred (as in the science bit we will assume you know the other part!). <br>" +
    "<br>" +
    "So here goes: the joining of your partner's sperm and your egg at the moment of conception resulted in a ball of cells (or a blastocyst, if you want to get really technical) which is now starting the 6 day journey from the fallopian tube to the uterus where it will attach itself to the lining of your womb and in nine months time, will result in a real live baby in your arms! <br>" +
    "<br>" +
"Amazingly, although you will not know for a few months (or until the birth) the <b><font color=#00690c >sex of your baby</font></b> has already been determined. The fertilised egg contains <b><font color=#e3007b>46</font></b> chromosomes with <b><font color=#e3007b>23</font></b> from you and <b><font color=#e3007b>23</font></b> from the dad. You as mum will always provide the <b><font color=#e3007b>X</font></b> chromosome but the dad can provide an <b><font color=#e3007b>X</font></b> or <b><font color=#e3007b>Y</font></b> chromosome. <b><font color=#e3007b>XX</font></b> and you will be having a girl, <b><font color=#e3007b>XY</font></b> and you will have a little boy in 9 months time!"

let attrStr = try! NSAttributedString(
    data: string.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
    options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)

答案 2 :(得分:0)

你可以使用更少耦合和更易编译的表达式

let dataStr: String = [
    "Your baby in week three",
    "Three weeks into your pregnancy, and you might be wondering just how this little miracle occurred (as in the science bit we will assume you know the other part!). \n",
    "So here goes: the joining of your partner's sperm and your egg at the moment of conception resulted in a ball of cells (or a blastocyst, if you want to get really technical) which is now starting the 6 day journey from the fallopian tube to the uterus where it will attach itself to the lining of your womb and in nine months time, will result in a real live baby in your arms!",
    "Amazingly, although you will not know for a few months (or until the birth) the <b><font color=#00690c >sex of your baby</font></b> has already been determined. The fertilised egg contains <b><font color=#e3007b>46</font></b> chromosomes with <b><font color=#e3007b>23</font></b> from you and <b><font color=#e3007b>23</font></b> from the dad. You as mum will always provide the <b><font color=#e3007b>X</font></b> chromosome but the dad can provide an <b><font color=#e3007b>X</font></b> or <b><font color=#e3007b>Y</font></b> chromosome. <b><font color=#e3007b>XX</font></b> and you will be having a girl, <b><font color=#e3007b>XY</font></b> and you will have a little boy in 9 months time!"
].joined(separator: "\n")