How to append all elements of an array to a label through a for loop in Swift

Max Goddard

I have an array weekOneExerciseArray that has 4 strings inside of it. I have a label that I want to assign each of these strings to, each on a new line. At the moment I've just written the individual array elements before attempting to write a more optimised version. However I get

Cannot subscript a value of type '[String]' with an index of type

on the exerciseString = "\(exerciseString)\n (weekOneExerciseArray[exerciseCount])"; } line.

I've tried so many variants to get this working but to no luck. I would be very thankful for any help!

let weekOneExerciseArray = ["Reverse Crunch", "Crunch", "Oblique Crunch", "Rope Crunch"];

//Week 1: 4 exercises
exerciseTextLabel.text = "\(weekOneExerciseArray[0])\n\(weekOneExerciseArray[1])\n\(weekOneExerciseArray[2])\n\(weekOneExerciseArray[3])";


var exerciseString = "";

for exerciseCount in weekOneExerciseArray
{
    exerciseString = "\(exerciseString)\n\(weekOneExerciseArray[exerciseCount])";
}

exerciseTextLabel.text = exerciseString;
Ahmad F

The reason for getting the error is in the for-in you are trying to access an element of an array by using a string as an index:

for exerciseCount in weekOneExerciseArray
{
    exerciseString = "\(exerciseString)\n\(weekOneExerciseArray[exerciseCount])";
}

At this point, exerciseCount is a string but not an integer, so what you should do instead is:

for i in 0..<weekOneExerciseArray.count {
    exerciseString = "\(exerciseString)\n\(weekOneExerciseArray[i])"
}

and you would be good to go.


A Better Solution

However, I would highly recommend to follow the following approach instead:

You could group up weekOneExerciseArray by joining its elements using joined method for generating the desired concatenation:

let weekOneExerciseArray = ["Reverse Crunch", "Crunch", "Oblique Crunch", "Rope Crunch"]

let concatenatedString = weekOneExerciseArray.joined(separator: "\n")
print(concatenatedString)
/*
 Reverse Crunch
 Crunch
 Oblique Crunch
 Rope Crunch
 */

exerciseTextLabel.text = concatenatedString

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to loop through elements of array of <UnsafeMutablePointer> in Swift

Append PFObject to Array resulting all elements replaced (Swift, Parse)

How to remove an all the elements in an array through iteration

How to loop through an array of objects in swift

Loop through all the elements of an array into jsonb and modify values

Loop through all the elements of an array into jsonb and delete keys

Loop through csv file and save all unique elements of a column into an array

Swift how to read through all indexes of array using for loop with index [i]

How to use all the elements of the array using for loop?

How to show all the elements of an array in swift?

How to append a "label" to a numpy array

How to loop through json and append inside append

How to append elements into an array from another View Controller in Swift

How can I loop through all the elements in my reactjs component?

How to loop through all the elements returned from getElementsByTagName

Javascript: How to loop through ALL DOM elements on a page?

Loop through array with missing elements

Loop through array and associated elements

How to append elements to a list with a loop?

How to loop through child elements and create nested array of objects?

How to loop through a bash array, excluding certain initial elements?

How can I loop through the elements of the multidimensional array and search for a match?

how to loop through an elements in jquery if the name attribute is an array?

How to loop through 1D array elements multiple times?

How to loop through an array for subset of elements (in reverse order) in perl

How to append elements into a dictionary in Swift?

How to loop through all UIButtons in my Swift view?

Loop through string array and change label text

How to compare one array element with all others array elements in a for loop?