1

I am trying to added elements to an empty string array and I tried to follow this post add-value-to empty-array but none the options are helping me as they result in Xcode throwing errors each time. here is the code if have tired:

var tasks = [String]()
tasks += ["something"]

This gave me 6 errors on x code with the first being Consecutive declaration on a line must be separated by ; then it says it's an invalid redeclaration of tasks followed by a bunch of errors saying to make it a func. When I try the .append func instead of += it gives the same errors

Now if I try this:

 var tasks = [String]()
 var tasks = ["Something"]

it only gives me the invalid redeclaration error but I do not believe this the correct way to add elements to the array

Hopefully, this helps explain my issue and sorry for the weird beginner question but thank for the help in advance

8
  • Can you try here: online.swiftplayground.run , I just run your first code snippet and tried to print(tasks) there is no compilation issues (Swift 5.1) Commented Feb 13, 2020 at 21:07
  • Your first snippet compiles on my machine. Your second snippet does have an issue, since you are re-declaring tasks. Just omit the second var and it should compile. Commented Feb 13, 2020 at 21:10
  • below the answer in the comments, I posted a link a Pastebin with all my code. here is an image with all the errors I got imgur.com/a/02nXoE4 Commented Feb 13, 2020 at 21:18
  • You should use: tasks += "something", or tasks.append(contentsOf: ["something"]) Commented Feb 13, 2020 at 21:21
  • You can't call a function or assign variables like that outside a function, move tasks.append(["something"]) inside viewDidLoad Commented Feb 13, 2020 at 21:23

2 Answers 2

1

I looked at the code in your pastebin and the issue is that you had both the declaration and assignment on separate lines in the class definition.

class TableViewController: UITableViewController {

    //temp list of tasks
    var tasks = [Sting]()

    //giving some default values in the cell
    tasks.append(["something"])

You also spelled String wrong, but that is not relevant for the fix.

Another issue is a type mis-match. You declare an array of String, which would be [String]. However, you are attempting to add an array of String to an another array of String, which is wrong.

tasks.append(["something"])

Instead, you should have

tasks.append("something")

This now adds an element of String to your array of Strings.

Finally, you can do one of two things: Assign the array at creation

var tasks = ["something"]

or assign it inside a function, like your ViewDidLoad

Sign up to request clarification or add additional context in comments.

Comments

-1

You can't use += with a [String] (array of Strings) and String.

Here's an example I ran in a playground:

var array: [String] = []
array.append("A")
print(array)

It prints ["A"]. Without seeing your code it will be hard to diagnose if there is another problem.

Update after looking at your code:

var tasks = [Sting]() // Should be String

tasks.append(["something"])

You can't append in the declaration, you'll need to add the append to a function (try viewDidLoad or viewWillAppear to test). ["something"] is an array of String, not a String. You'll need to use "something" instead.

1 Comment

ok, I updated it to use append still got the same errors thrown here is a Pastebin with all of my code in this swift file. pastebin.com/11YHV7Zg

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.