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

Community
  • 1
  • 1
LucyEly
  • 63
  • 1
  • 13
  • Can you try here: http://online.swiftplayground.run/ , I just run your first code snippet and tried to `print(tasks)` there is no compilation issues (Swift 5.1) – denis_lor Feb 13 '20 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. – Joshua Kaden Feb 13 '20 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 https://imgur.com/a/02nXoE4 – LucyEly Feb 13 '20 at 21:18
  • You should use: `tasks += "something"`, or `tasks.append(contentsOf: ["something"])` – koen Feb 13 '20 at 21:21
  • You can't call a function or assign variables like that outside a function, move `tasks.append(["something"])` inside `viewDidLoad` – Joakim Danielson Feb 13 '20 at 21:23
  • thanks that worked I did not realize I could not define it globally. Now I forget which one of the override tableView functions is the didLoad one – LucyEly Feb 13 '20 at 21:26
  • You can define variables globally on the class level, they are then called properties, and initialise them with a value but you can not change the value of them there. That needs to be done in a function – Joakim Danielson Feb 13 '20 at 21:29
  • You're adding an array of strings to an array of strings! You could say like `tasks += "something"` or tasks.append("something"). What you're trying to do is purely a syntactical error. – Pierce Feb 13 '20 at 22:59

2 Answers2

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

Wyetro
  • 8,439
  • 9
  • 46
  • 64
CodeBender
  • 35,668
  • 12
  • 125
  • 132
-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.

Wyetro
  • 8,439
  • 9
  • 46
  • 64
  • 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. https://pastebin.com/11YHV7Zg – LucyEly Feb 13 '20 at 21:14