swift compiler shows Expected declaration error?
I think you have the code in the wrong place in the class same like this question.
so move it to any function or in viewDidLoad
method.
Hope it will help.
You have the code like below image:
Seems like Your code is outside the function. If allListViewController
is your UIViewController
class where the for
loop code is written make sure the code should be inside the body of any function of allListViewController
class. It can not be outside.
Example:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
for list in lists{
let item = ChecklistItems()
item.text = "Item for (list.name))"
list.items.append(item)
}
}
You can just initialise/declare the variables (will be global variables) outside the function body.
When we add code which should be added inside some function and then call , At that time it will show error: expected declaration
Because only Property Declaration should come directly inside class . So when we use any property then it should happen in some function .
Here is an example of Activity Indicator class I have created.
Error when code is written outside.
It will work when written inside function.