I don't understand what's wrong with this code in my View Controller, the very bottom line (with the single } bracket) keeps getting two errors, one that states "Consecutive declarations on a line must be separated by ';'" and "expected declaration". When I add the semicolon where it directs me to it still says an expected declaration error....but for what? Can anyone find anything wrong with it?
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var testObject = PFObject(className:"TestObject")
testObject["foo"] = "bar"
testObject.saveInBackgroundWithTarget(nil, selector: nil)
var voteCount = PFObject(className:"VoteCount")
voteCount["votes"] = 0
voteCount["optionName"] = "Crepes"
voteCount.incrementKey("votes")
voteCount.saveEventually()
var query = PFQuery(className:"VoteCount")
query.getObjectInBackgroundWithId("e8KhneiSfw") {
(voteCount: PFObject!, error: NSError!) -> Void in
if error != nil {
NSLog("%#", error)
} else {
voteCount["votes"] = 1
voteCount.incrementKey("votes")
voteCount.saveEventually()
}
}
class Counter {
var voteCount: Int = 0
func incrementBy(amount: Int, numberOfTimes times: Int) { voteCount += amount * times
}
}
func didReceiveMemoryWarning() {
didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
There's a missing closing brace before this line:
class Counter {
Your viewDidLoad() method is not properly closed, so what happens is that the class and didReceiveMemoryWarning are defined as local to viewDidLoad.
A proper indentation usually reveals errors like that... are you properly indenting your code?
As written, class Counter and func didReceiveMemoryWarning() are inside viewDidLoad. Fix your braces.
Related
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func declareName() {
print("hello")
}
declareName() //I get an error here saying :
}
Expected '{' in body of function declaration
I was working on something complex and it wasn't working. So i tried the very basics and I got this error. Any fixes?
All executable code must live in a function body. Therefore, in answer to your comment, yes, this code is illegal:
func add (a: Int) { print(a) }
add(5)
You would have to say:
func add (a: Int) { print(a) }
func callAdd() { add(5) }
I have just started to code, but I have already encountered some error which I cannot figure out. Would you please help me?
class ViewController: UIViewController {
#IBOutlet var getNumber: UITextField!
#IBAction func computePrime(_ sender: AnyObject) {
if let userString = getNumber.text { // Convert input to Int
let userNumber = Int(userString)
if let number = userNumber {
var i = 2 // Variable declaration
var prime = true
while i < number { // Prime calculation
if number & i == 0 {
prime = false
i = number
} else {
i += 1
}
}
if prime == false { // Output result
displayResult.text = "It is not prime"
} else {
displayResult.text = "It is prime"
}
} else {
displayResult.text = "Please enter a positive whole number" // Error message in case value entered is not good
}
}
}
#IBOutlet var displayResult: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
When I try to run it, the app crashes and it gives me this error:
"Thread 1: signal SIGABRT"
highlighting this line:
"class AppDelegate: UIResponder, UIApplicationDelegate {"
from the AppDelegate.swift file, which I have not modified.
How can I fix it?
Thank you!
Probably you don't have connected in Storyboard the getNumber and displayResult (IBOutlet) object.
Check that answer:
IBOutlet not connecting in SWIFT
The highlighted line is at the application delegate because the system regressed all the way back to the top trying to find a layer with an exception handler for the exception that was thrown. Check the last few lines of the Xcode console for a description of the exception which may tell you where it was caused.
You can add an "Exception Breakpoint" to stop execution at the moment of the exception to see the point of origin. Many articles exist on how to do this.
This is my first time doing a simple project in swift and these errors are bugging me for last couple of hours. I have this code below and even though i have curly braces around and statements inside the if/else i still get that errors. Any help would be greatly appreciated guys.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var `switch`: UISwitch!
#IBOutlet var Answer: UILabel!
#IBOutlet var tempInput: UITextField!
//aqnswer value
#IBAction func switchPressed(sender: AnyObject)
{
if switch.on {
self.Answer.text = "cel to fah"
}
else {
self.Answer.text = "fah to cel"
}
}
//textfield value
#IBAction func calculate(sender: AnyObject)
{
//get user input
// value = celcius
var Value:Int = tempInput.text.toInt()!
var toFah :Int = ( 32 + Value * 9 ) / 5
//to celcius
var toCel: Int = (Value-32) * 5 / 9
if switch.on {
self.Answer.text = toFah.description
}
else {
self.Answer.text = toCel.description
}
// println(fah)
// Answer.text = fah.description
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The Swift Language Guide says:
If you need to give a constant or variable the same name as a reserved
Swift keyword, surround the keyword with back ticks (`) when using it
as a name. However, avoid using keywords as names unless you have
absolutely no choice.
In your example you have indeed a choice…
But if you really really really want to use switch as a variable name you have to wrap all occurrences of switch in back ticks.
My code looks as follows, and it is the code I have borrowed from the quick start tutorial, at Parse.
import UIKit
import Parse
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let testObject = PFObject(className: "TestObject")
testObject["foo"] = "bar"
testObject.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
println("Object has been saved.")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I'm getting the error at the "testObject["foo"] = "bar"" line. Why is that?
I have also set up the database key in the AppDelegate.swift file
Again, the error says the following: 'PFObject' does not have a member named 'subscript'
This is because you are using older version of Parse.framework.
Take a look at Parse change log and make sure you are using the latest version.
I have an error when I typed in some code, it gave me an error. It looks perfect, but here is the code:
import UIKit
class SecondViewController: UIViewController {
#IBOutlet weak var resultLabel: UILabel!
#IBOutlet weak var tempText1: UITextField!
#IBAction func convertTemp1(sender: AnyObject) {
#let fahrenheit = (tempText1.text as NSString).doubleValue
let celsius = (fahrenheit - 32 )/1.8
let resultText = "Celsius \(celsius)"
resultLabel.text = resultText
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
It give me the following error: 'String?' is not convertable to 'NSString' On the line I put a # on. The # is not in the real code.
Note: I do not understand much computer talk so please try to speak very simply. :)
The text property of UITextField returns an optional string but your code doesn't handle optionals. Casting to NSString isn't allowed there (also not necessary to get to the doubleValue).
You need to handle the optional. For this you could force-unwrap it using !. But that can lead to crashes. It would be better to use the if let or guard let statements:
guard let fahrenheit = tempText1.text?.doubleValue else { return }
For conciseness we use optional chaining (the ?here). We could also keep this in two steps:
guard let fahrenheitString = tempText1.text else { return }
let fahrenheit = fahrenheitString.doubleValue
Both are basically equivalent.