0

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.

2 Answers 2

2

You have to declare the array in this way:

var tasks = [task]()

It changed in swift from the tutorial you are watching.

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

1 Comment

Both answers helped but I felt this one was easier to understand. I will vote up both but accept this answer. Thanks @Dario.
2

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]()

Comments

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.