4

I'm learning Swift and found one wonderful tutorial where explained how to create card game. Point is that we use 14 cards with card images and image files are named card0, card1... etc.

So, we have to create array of strings with these filename. Teacher does it with hardcoding like

var cardNamesArray:[String] = ["card1","card2","card3","card4","card5","card6","card7","card8","card9","card10","card11","card12","card13"]

I want to do it with the for-in. But when I try to do like this:

for i in 0...13 {
var cardNamesArray:String = [String(format: "card%i", i)]
}

I get error in XCode on line with for.... "Expected declaration".

Could you please point me where I'm wrong or if there is another way to do it.

2
  • 1
    "Teacher does it with hardcoding like..." Your teacher should be fired. Commented Aug 26, 2015 at 17:41
  • cardNamesArray should be a let. It is a bunch of fixed names. Commented Mar 20, 2017 at 20:46

3 Answers 3

9

Using map() this could be written:

var cardNamesArray = (0...13).map{"card\($0)"}
Sign up to request clarification or add additional context in comments.

7 Comments

Wait, you don't need the [Int], since you're not trying to get an array of Ints, but Strings.
The Int array is mapped to the String array. In long form it would be written: let intArray = [Int](0...13); var cardNamesArray = intArray.map{"card\($0)"}, but it can also be all strung together as I've done.
Oh, oh, I get it. But isn't the [Int] implied? What other type can (0...13) be?
Thank you so much. It worked. Could you please explain how [Int](0...13) works? Thank you in advance.
@Senator14 the [Int](0...13) creates an array of Int, i.e. [0,1,2,3,4,5,6,7,8,9,10,11,12,13] but as @NRitH points out the [Int] part can be skipped, so I'll edit that out.
|
6

Here is the solution. You first need to initialize an empty string array before the for loop. You then iterate from 1 to 13, not 0 to 13 since it will include 0. You then append the string to the array. Your string formatting was incorrect so notice the way to format it in Swift.

var cardNamesArray = [String]()

for i in 1...13 {
    cardNamesArray.append("card\(i)")
}

5 Comments

Dear @Kris-Gellci thank you so much. But I still have error "Expected declation" on line with "for i in 1...13 {" Why this could happen? Thank you in advance.
Are you using Playgrounds or are you in an actual project?
I'm in my actual project
Which Xcode are you using? I tested and it works fine in the latest Xcode 7 beta. You could also clean the project, command+shift+k, then build again when it is done cleaning. Command+b
I use Xcode 6.4 I tried cleaning, but it didn't help
4

If I understand the answer correctly, this is the solution:

var cardNamesArray: [String] = []
for i in 0...13 {
    cardNamesArray.append("card\(i)")
}

You need to initialise the array once in your program (before filling it), then fill it in for loop.

You also can init your array this way:

var cardNamesArray = [String](count: 14, repeatedValue: "")

This will allocate memory for 14 items of the array, which is better than calling .append() many times.

var cardNamesArray = [String](count: 14, repeatedValue: "")
for i in 0...13 {
    cardNamesArray[i] = "card\(i)"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.