Expected Declaration Error Creating Array in Viewcontroller, Can't Work Out Why

Expected Declaration error creating array in ViewController, can't work out why

You can only have property declarations outside of methods in a class. All functionality of the class goes inside of methods. When you declare var icon = UIImage[]() outside of a method, it's an instance property declaration and is valid code.

Your next two lines attempt to modify the property. Code outside of methods is never executed because there is no way to call it. While you can declare properties outside of methods, you have to use them inside a method in your class.

I'd recommend learning more about Object Oriented programming, because it seems like you don't quite have the grasp of it yet. You may want to try a language that has more reliability and learning resources than swift currently does. If you are planning on doing iOS development, it would be helpful to learn objective-c even if you want to use Swift because you'll gain exposure to Apple's APIs which are the same in both languages.

swift expected declaration error

You need to pass your name variable to some kind of method in order for it to work such as viewDidLoad.

@IBOutlet weak var name: UITextField!

override func viewDidLoad() {

name

}

Expected declaration error at the do { line using Swift 3

Your main problem is that you're attempting to use a do statement in the body of a class, whereas it should only be in the body of a function. For the sake of the argument, I'll put this in the viewDidLoad method.

class Calculator: UIViewController {

var display = "0"
var numerator : Float?
var denominator : Float?
var total : Float?

enum divisionErrors: Error {
case inf
case nan
}

func divide(num: Float, by denum: Float) throws -> Float {
guard num != 0 else{throw divisionErrors.nan}
guard denum != 0 else{throw divisionErrors.inf}

let computedValue = num / denum
return computedValue
}

override func viewDidLoad() {

do {
try total = divide(num: numerator!, by: denominator!)

} catch divisionErrors.inf {
print("Error")
display = "0"
} catch divisionErrors.nan {
print("Error")
display = "0"
} catch {
assert(false, "Other Error")
}
}
}

In other words, when you do something, you must also try to do something that could fail. Afterwards, you close the do with some catch statements on your errors. Like a switch statement, your catch cases must be exhaustive. You will have errors when trying to compile without that final catch rounding up the rest of the possible errors, as potential errors transcend the scope of your divisionErrors enum.

You might also want to error check numerator and denominator to be sure they exist. I just force unwrapped them for the sake of the exercise.

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.

Getting Expected declaration error when using emoji as variable name

There is an explicit list of allowed Unicode characters in identifiers in the Lexical Structure chapter of Swift Language Reference (look for "GRAMMAR OF AN IDENTIFIER").

"⛽" = U+26FD is not in the list.

Why do I keep getting this error when going from ViewController to TableViewController?

When I got this error, I had initially used the boilerplate code for the class UITableViewController, but the actual view controller was a UIViewController.

Original code (resulting in the error):

Note that this is connected to a UIViewController in Storyboard.

class MainViewController: UITableViewController {
//insert rest of code here
//note that funcs such as numberOfRowsInSection(_:) will have the override keyword
}

Working code (removing the error):

class MainViewController: UIViewController {
//insert code here
//you also must *remove the override keywords* for
// some of the included functions or you will get errors
}

Remember also to reference an IBOutlet for your UITableView in your view controller, and set the delegate and datasource (in Storyboard, you Ctrl+Drag from the UITableView to the yellow circle at the top of the view controller, and click dataSource. Repeat this for the delegate as well).

Weird Switch error in Obj-C

You can't declare variables right after a case label.



Related Topics



Leave a reply



Submit