Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift compiler shows Expected declaration error? [duplicate]

Tags:

When this code is written in AllListViewController and run, the compiler shows the Expected Declaration error:

for list in lists{     let item = ChecklistItems()     item.text = "Item for \(list.name))"     list.items.append(item) }        
like image 456
Ashwini Avatar asked Aug 07 '15 09:08

Ashwini


2 Answers

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.

like image 153
Dharmesh Kheni Avatar answered Sep 24 '22 18:09

Dharmesh Kheni


You have the code like below image:

enter image description here 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.

like image 34
TheTiger Avatar answered Sep 20 '22 18:09

TheTiger