Unresolved_Identifiers

I am new to Swift coding. Since my application will use extensively Forms, Tables and Database I am using a turorial from techtopia.com to help me get started -- "An Example SQLite based iOS 10 Application using Swift and FMDB" --

https://www.techtopia.com/index.php/An_Example_SQLite_based_iOS_8_ Application_using_Swift_and_FMDB


The problem is that after I followed all the instructions I am still getting errors. Can anyone help to know what I need to do in the ViewController.swift file to address the errors?


The Errors are below:

let contactDB = FMDatabase (path: databasePath as String) -- Use of unresolved identifier 'FMDatabase'


@IBAction func saveData(_ sender: AnyObject) { -- Only instance methods can be declared @IBAction


let contactDB = FMDatabase(path: databasePath as String) -- Use of unresolved identifier 'FMDatabase'; Use of unresolved identifier 'databasePath'


let insertSQL = "INSERT INTO CONTACTS (name, address, phone) VALUES ('\(name.text!)', '\(address.text!)', '\(phone.text!)')" --

Use of unresolved identifier 'address' ; Use of unresolved identifier 'name' ; Use of unresolved identifier 'phone'


if !result! {

status.text = "Failed to add contact"

print("Error: \(contactDB?.lastErrorMessage())")

} else {

status.text = "Contact Added" -- use of unresolved identifier 'status'

name.text = "" -- use of unresolved identifier 'name'

address.text = "" -- use of unresolved identifier 'address'

phone.text = "" -- use of unresolved identifier 'phone'



The same errors show up where ever the above code is used.

I am using an iMAC Mojave v. 10.14.3 with Xcode version 10.1 and Swift 5

Answered by DTS Engineer in 355356022

[For those following along at home, this is a correct link to the tutorial.]

Use of unresolved identifier 'FMDatabase'

To fix this you’ll need to import the code that includes the

FMDatabase
type. Reading through that tutorial it suggests using the bridging header approach. Did you follow those steps?

Only instance methods can be declared @IBAction

It’s hard to answer this without more context, but it seems that you’re trying to declare your action method is a standalone function rather than as a method. The declaration needs to be inside your view controller type. For example:

class ViewController … {
    … stuff …
    @IBAction func saveData(_ sender: AnyObject) {
        … more stuff …
    }
    … even more stuff …
}

Use of unresolved identifier 'address' ; Use of unresolved identifier 'name' ; Use of unresolved identifier 'phone'

I need more context to give you a meaningful answer. Try fixing the above two issues and let us know how you got along.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Accepted Answer

[For those following along at home, this is a correct link to the tutorial.]

Use of unresolved identifier 'FMDatabase'

To fix this you’ll need to import the code that includes the

FMDatabase
type. Reading through that tutorial it suggests using the bridging header approach. Did you follow those steps?

Only instance methods can be declared @IBAction

It’s hard to answer this without more context, but it seems that you’re trying to declare your action method is a standalone function rather than as a method. The declaration needs to be inside your view controller type. For example:

class ViewController … {
    … stuff …
    @IBAction func saveData(_ sender: AnyObject) {
        … more stuff …
    }
    … even more stuff …
}

Use of unresolved identifier 'address' ; Use of unresolved identifier 'name' ; Use of unresolved identifier 'phone'

I need more context to give you a meaningful answer. Try fixing the above two issues and let us know how you got along.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

An error like:


@IBAction func saveData(_ sender: AnyObject) { -- Only instance methods can be declared @IBAction


maybe due to an extra closing curly brace that puts the IBAction out of the class body.

Check the balances of all curly braces before the IBAction line.


To help further, we would need to see the complete class' code

Hello Eskimo,

I have been doing some study on coding Swift. Still have problems. I did read about the bridging header and thought I completed that step. I don't know if it will help but I have attached three screen shots showing the errors from my ViewController screen. Seems that I can send only one screenshot so I send the ither two separately.


I have been trying to send some screenshots but thus far have been unsuccessful. The code itself has too many characters. I there another way to send a screenshot?

Below is a set of errors: Maybe someone can identify the problem with this code: (Unresolved Identifier Underlined)


@IBAction func saveData(_ sender: AnyObject) {

let contactDB = FMDatabase(path: databasePath as String)

if (contactDB?.open())! {

let insertSQL = "INSERT INTO CONTACTS (name, address, phone) VALUES ('\(name.text!)', '\(address.text!)', '\(phone.text!)')"

let result = contactDB?.executeUpdate(insertSQL,

withArgumentsIn: nil)

if !result! {

status.text = "Failed to add contact"

print("Error: \(contactDB?.lastErrorMessage())")

} else {

status.text = "Contact Added"

name.text = ""

address.text = ""

phone.text = ""

}

} else {

print("Error: \(contactDB?.lastErrorMessage())")

}

}

> trying to send some screenshots but


See FAQs 1 & 2 here: For Best Results - Read the Label

Looks like you did not install every component needed.


Have a look at tutorials to check precisely how to proceed.


such as this (a bit old) one : h ttps://www.appcoda.com/fmdb-sqlite-database/

Hello Eskimo:


I have thus far resolved most of the errors. Two errors persist (Underlined) as follows:

#1-->

ERROR: "=" Comparing non-optional value of type 'FMDatabase' to 'nil' always returns false

if !filemgr.fileExists(atPath: databasePath as String) {

let contactDB = FMDatabase(path: databasePath as String)

if contactDB == nil{

print("Error: \(contactDB.lastErrorMessage())")




#2--> "If"

ERROR: Expected declaration

if (contactDB.open())! {

let querySQL = "SELECT address, phone FROM CONTACTS WHERE name = '\(name.text!)'"

I have thus far resolved most of the errors.

That’s great to hear.

#1 … Comparing non-optional value of type 'FMDatabase' to 'nil' always returns false

My best guess here is that your tutorial is out of date:

  1. It was written at a time when the

    FMDatabase
    initialise might fail, and thus returns an optional
    FMDatabase
    .
  2. Hence, the author added code to check for the

    nil
    case.
  3. Since then

    FMDatabase
    has changed to not return an optional value, and thus this check is triggering an error.

Regardless, the way forward is clear: If

contactDB
is not optional, you don’t need to check for
nil
and thus you can delete the
if
statement and everything inside it.

#2 … ERROR: Expected declaration

It’s hard to say what’s going on here without seeing the code before the error. If that code is the code from #1, fixing #1 may well fix that. If not, post a slightly large code snippet.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hello:


I deleted the "if" statement (from the previous post) and the error went away. When I build it I will let you know what happens.


Here is some more code: I put a comment where the error shows up


@IBAction func saveData(_ sender: AnyObject) {

let contactDB = FMDatabase(path: databasePath as String)

if (contactDB.open()) {

let insertSQL = "INSERT INTO CONTACTS (name, address, phone) VALUES ('\(Name.text!)', '\(Address.text!)','\(Phone.text!)')"

let result = contactDB.executeUpdate(insertSQL,

withArgumentsIn:[])

if !result {

Label.text = "Failed to add contact"

print("Error: \(contactDB.lastErrorMessage())")

} else {

Label.text = "Contact Added"

Name.text = ""

Address.text = ""

Phone.text = ""

}

} else {

print("Error: \(contactDB.lastErrorMessage())")

}

}

@IBAction func FindContact(_ sender: AnyObject) {

_ = FMDatabase(path:databasePath as String)

}

if (contactDB.open())! { // HERE IS WHERE THE ERROR : "Expected declaration" shows up.

let querySQL = "SELECT address, phone FROM CONTACTS WHERE name = '\(name.text!)'"

let results:FMResultSet? = contactDB?.executeQuery(querySQL,

withArgumentsIn: nil)

if results?.next() == true {

address.text = results?.string(forColumn: "address")

phone.text = results?.string(forColumn: "phone")

status.text = "Record Found"

} else {

status.text = "Record not found"

address.text = ""

phone.text = ""

}

contactDB?.close()

} else {

print("Error: \(contactDB?.lastErrorMessage())")

}

}

HERE IS WHERE THE ERROR : "Expected declaration" shows up.

You seem to have a rogue closing curly bracket

}
in your code. From the indentation it’s clear that you intended this
if
statement to be inside the
FindContact(_:)
method, but that method ends at the closing curly bracket on the previous line.

Note If you format your code as code blocks (using the

<>
icon), it’ll have line numbers and then I’ll be able to reference lines by number, which makes things easier.

I think you need to might be better off switching to a simpler tutorial. The kinds of errors you’re reporting on this thread indicate that you’re struggling with very basic Swift concepts, like balancing brackets and

Optional
. That tutorial is trying to teach you about more advanced concepts, namely, wrangling SQLite databases. You need to get a good handle on the basics before you tackle such advanced fare.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hello Eskimo:


Your suggestions were very helpful. You helped me resolved all the issues with the tutorial in my Xcode Project. I'll follow-up with your suggestion about finding a tutorial that will benefit me since I am just starting with Xcode and Swift. I specifically want to work with databases. I worked with Oracle many years and have an app built in Oracle Pl/SQL that I want to convert to iOS. Do you know of a tutorial or tutorials that help with using Swift and SQLite?


Thanks.

Do you know of a tutorial or tutorials that help with using Swift and SQLite?

No, sorry. I learnt SQLite before Swift existed, so I’ve no direct experience with such tutorials.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Unresolved_Identifiers
 
 
Q