Swift println不打印数组值

时间:2014-07-01 01:29:20

标签: ios xcode swift

我是全新学习的Swift,我想知道我在这里做错了什么?我喜欢玩代码来获得理解。

var shoppingList = ["pound of catfish", "bottle of fresh water", "bag of tulips", "can of blue paint"]

println("Susie checks her Shopping List to find that a \(shoppingList[2]) is her third item.")

我试图找出为什么输出没有说" Susie检查她的购物清单,发现一袋郁金香是她的第三个项目。"与目前所说的相反:完全如上所述," Susie检查她的购物清单,发现(shoppingList [2])是她的第三个项目。"

我知道这是一个超级基本的概念,除了我想确保我100%理解所有内容。

谢谢!

3 个答案:

答案 0 :(得分:1)

这里的问题是你只是输出字符串“(shoppingList [2])”。

要将其替换为您想要的值,您必须先使用\转义字符串。

var shoppingList = ["pound of catfish", "bottle of fresh water", "bag of tulips", "can of blue paint"]

"Susie checks her Shopping List to find that a \(shoppingList[2]) is her third item."

这称为字符串插值,您可以找到更多信息here

答案 1 :(得分:0)

\前面需要()

var shoppingList = ["pound of catfish", "bottle of fresh water", "bag of tulips", "can of blue paint"]

"Susie checks her Shopping List to find that a \(shoppingList[2]) is her third item."

String Interpolation

答案 2 :(得分:0)

与Apple文档一样:

  

“Swift使用字符串插值来包含常量或的名称   变量作为较长字符串中的占位符,并提示Swift   将其替换为该常量或变量的当前值。包裹   括号中的名称,并在之前用反斜杠转义它   开括号。“

在您的情况下,代码应为:

println("Susie checks her Shopping List to find that a \(shoppingList[2]) is her third item.")