Swift Compiler Shows Expected Declaration Error

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.

Swift Compiler Shows Expected Declaration Error

call your code in inside the scope

override func viewDidLoad() {
super.viewDidLoad()

let request = MKLocalSearchRequest()
request.naturalLanguageQuery = "bars"
request.region = regionRadius

}

or you can use as

 let request = MKLocalSearchRequest()
override func viewDidLoad() {
super.viewDidLoad()
request.naturalLanguageQuery = "bars"
request.region = regionRadius

}

Expected Declaration Error using Swift

The problem is that you need to put your code inside a method. All you need is to move it to viewDidLoad() or any other method.

Xcode shows a Expected declaration error with any variable put in

You need to move that assignment either to declaration or into valid scope:

1:

var cpuSting = "Intel i5"

2:

override func viewDidLoad() {
super.viewDidLoad()
cpuSting = "Intel i5"
}

What you do in your code is essentially attempting to make an assignment in an improper place. If you declare a variable, then just declare it with required initial value. If you want to re-assign a value, then just do that in the right place, such as in instance method scope like viewDidLoad.

Expected declaration when adding values to a Swift dictionary

You get this error because your line setValue cannot just live here inside your class without being inside a method. Of course here it really depends on what (and how) you want to accomplish, but you could put it in the init() method of your BookingSystem class, or you could build your own custom init().

Here is how it would look like:

    import Foundation

class Booking {

// Some interesting things here

}

class Table : NSObject {

// MARK: Properties

var tableID: Int
var tableCapacity: Int
var status: String

// MARK: Initializers

init(tableID: Int, tableCapacity: Int, status: String) {
self.tableID = tableID
self.tableCapacity = tableCapacity
self.status = status
}

}


class BookingSystem {

// MARK: Properties

var tablesBooked = [Int: String]()

var table = Table(tableID: 1 , tableCapacity: 2, status: "A")

var bookings = [Booking]()

// MARK: Initializers

init() {

// I am not sure what you are trying to do here, but anyway you should add it in a custom method or your init. If I were to use the code in your example, you would add this here:

tablesBooked[table.tableID] = table.status
}

// ...
}

I added the Table class here on purpose, just to show you an example on how to create your own custom init.

Also, another thing worth mentioning here is that Swift Dictionaries don't have a setValue:forKey: method. Instead, to add an object to your Dictionary, you should use:

yourDictionnary["yourKey"] = yourValue

Hope it helps, and if you have any questions just feel free asking :)

Expected declaration on switch declaration

You can't write switch rank { inside the struct directly , it should be inside a function init or any other custom one

public struct beltRank {
var rank = 0
var belt = ""
init(rank:Int) {
// write switch here
}
}


Related Topics



Leave a reply



Submit