I decided to learn Swift and I decided to start with Swift 2 right away.
So here is a very basic example that's similar to one of the examples from Apple's own e-book about Swift
let greeting = "Guten Tag"
for index in indices(greeting) {
print(greeting[index])
}
I tried this in the playground of Xcode 7 and I received the following error
Cannot invoke 'indices' with an argument list of type '(String)'
I also tried the same thing with Xcode 6 (which is Swift 1.2 AFAIK) and it worked as expected.
Now, my question is: Is this
- An error in Xcode 7, it's still a beta release after all, or
- Something that just doesn't work anymore with Swift 2 and the e-book just isn't fully updated yet?
Also: If the answer is "2", how would you replace indices(String)
in Swift 2?
In a Playground, if you go to menu View > Debug Area > Show debug area, you can see the full error in the console:
Also,
String
s do not conform toSequenceType
s anymore, but you can access their elements by callingcharacters
.So the solution for Swift 2 is to do it like this:
Result:
Of course, I assume your example is just to test
indices
, but otherwise you could just do: