Hi i need some help with my code. is an ld c-style line, and i have problem to convert it to swift 4.
func ausgabeBestand () {
for var index = 0; index <- Bestand.count; index++ {
print(Bestand[index])
print("Insgesamt befinden sich (index+1) Kisten im Lager.")
}
Welcome to the Swift forums! Rather than someone writing the code for you, I’d recommend checking out the Swift books section on loops. It’ll be a lot more helpful to have a solid understanding of how loops and ranges work in Swift. Control Flow — The Swift Programming Language (Swift 5.7)
It's hard to get an idea of what you were trying to build, but maybe this will help you:
func ausgabeBestand() {
for index in 0 ..< bestand.count {
print(bestand[index])
print("Insgesamt befinden sich \(index + 1) Kisten im Lager.")
}
}
If you wish for more help you should post code written in English (translating using Google Translator would be enough). To better showcase an issue consider using markdown for better readability. Have a good day coding ;)
PS: C-Style for loops were removed from the language two years ago in favor of a modern for in (where) loops.
For me the example in C does not make sense because the summary ("Insgesamt befinden sich...) will be printed after every item (german Kisten in this case) of bestand, so I moved this out of the loop.
And assuming that bestand is an array (or any other sequence) the for loop can be simplified:
func ausgabeBestand() {
for kiste in bestand {
print(kiste)
}
print("Insgesamt befinden sich \(bestand.count) Kisten im Lager.")
}
I kept it as is since the example was ill-formed anyway and I did not want to guess it, but in terms of functionality your example is the right way to go.
Well I wouldn't like to see this forum to turn into a multilingual mess. It's counterproductive if you want to help or contribute to a discussion but do not know the language the people in that particular thread speak. I can speak three languages myself, including German, but I'd appreciate if the community of this forums would stick to the international language.
That said, speaking in a different language is not banned here, but I guess it's fair if we kindly advertise that English is still preferred. However I only speak for myself.
func ausgabeBestand()
{
for (i, value):(Int, Bestand) in bestand.enumerated()
{
print(value)
print("Insgesamt befinden sich \(i + 1) Kisten im Lager.")
}
}