简体   繁体   English

如何在Swift中将int值附加到字符串数组中?

[英]How do I append an int value into a string array in Swift?

I'm learning Swift now (with basic programming know-how), and I'm making a String array containing a deck of 52 cards via a for loop, but I'm not sure how to append a value with an int and string values. 我现在正在学习Swift(具有基本的编程知识),并且正在通过一个for循环制作一个包含52张卡片的String数组,但是我不确定如何在int和string后面附加值价值观。

I know using \\(int) converts an int to a string, but it doesn't seem to work when appending to an array. 我知道使用\\(int)将int转换为字符串,但是在追加到数组时似乎不起作用。 My code, including the error messages are below: 我的代码(包括错误消息)如下:

var suits = ["Spades", "Hearts", "Clubs", "Diamonds"]

var deck:[String] = []    

for s in suits
{

    deck.append("Ace of " + s)
    deck.append("King of " + s)
    deck.append("Queen of " + s)
    deck.append("Jack of " + s)

    for (var i = 2; i < 11; ++i)
    {
        deck.append(\(i) + " of " + s) 
        //error message: "Expected ',' separator"
        //error message: "Invalid character in source file"
    }
}

You need to have your (i) in quotes in order for it to be converted to a String. 您需要将(i)用引号引起来才能将其转换为字符串。

deck.append("\(i)" + " of " + s) 

You could also do this: 您也可以这样做:

var value = String(i)
deck.append(value + " of " + s) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM