Bigfoot11
Bigfoot11

Reputation: 921

Empty Array in Swift?

I am trying to create an empty array in Swift that the user adds on to. It is a very small class because I am just starting a new file. I am using this tutorial for my project which is what the code is based off. Here is the code I tried (It didn't work):

var tasks = task[]()

Here is all of my code in case it is needed:

class TaskManager: NSObject {

    var tasks = task[]()

    func addTask(name: String){
        tasks.append(task(name: name))
    }
}

There is an error on the var tasks = task[]() line saying: "Array types are now written the brackets around the element type". I am unsure of how to fix the problem.

How can one create an empty array?

Any input or suggestions would be greatly appreciated.

Thanks in advance.

Upvotes: 0

Views: 420

Answers (2)

Dario
Dario

Reputation: 3135

You have to declare the array in this way:

var tasks = [task]()

It changed in swift from the tutorial you are watching.

Upvotes: 2

Connor
Connor

Reputation: 64674

The syntactic sugar for an array of Type is [Type] in Swift. You can create an empty array of Tasks like this:

class Task{}
var tasks = [Task]()

Upvotes: 2

Related Questions