Swift FMDB代码解释

Swift FMDB Code Explanation

我是Swift和FMDB的初学者,我从互联网上的资源中得到下面的代码,并尽力理解代码。我在说明我认为它正在做的事情的声明下方发表了评论。带问号的没看懂

class ViewController: UIViewController {
@IBOutlet weak var name: UITextField!
@IBOutlet weak var specialty: UITextField!
//Defines name and specialty as contents of text fields

var dbpath = String()
//defines the database path
func getPath(fileName: String) -> String {

    let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    //finds document and returns an array of paths
    let fileURL = documentsURL.URLByAppendingPathComponent(fileName)
    print(fileName)
    //finds path to fileName with URLByAppendingPathComponent


    print("File Path Is : \(fileURL)")

    return fileURL.path!
    //returns the fileURL in path format?????
}


//Button "Add Shop" definition
override func viewDidLoad() {
    super.viewDidLoad()

    let dirPaths =
    NSSearchPathForDirectoriesInDomains(.DocumentDirectory,
        .UserDomainMask, true)
    //creates search paths for directories, then ?????

    let docsDir = dirPaths[0] 

    let dbPath: String = getPath("shopdata.db")
    //assigns string "shopdata.db" to dbPath
    let fileManager = NSFileManager.defaultManager()
    //easier access for NSFileManager, returns shared file for the process when called

    if !fileManager.fileExistsAtPath(dbPath as String) {
        //if there is already a database, do the following

        let contactDB = FMDatabase(path: dbPath as String)
        //contact database with path identified in function getPath

        if contactDB == nil {
            print("Error: \(contactDB.lastErrorMessage())")
            //If there is no database
        }

        if contactDB.open() {
            let sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, SPECIALTY TEXT, NAME TEXT)"
            if !contactDB.executeStatements(sql_stmt)
                //executes a create table statement as defined above
            {
                print("Error: \(contactDB.lastErrorMessage())")
                //if cannot execute statement, display error from fmdb
            }
            contactDB.close()
                //close connection
        } else {
            print("Error: \(contactDB.lastErrorMessage())")
            //if contact cannot be made, display error from fmdb
        }
    }
}

@IBAction func addShop(sender: AnyObject) {
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

此函数将从 DocumentDirectory 中获取给定文件名的文件路径,然后return返回。

func getPath(fileName: String) -> String {

let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
//finds document and returns an array of paths
let fileURL = documentsURL.URLByAppendingPathComponent(fileName)
print(fileName)
//finds path to fileName with URLByAppendingPathComponent


print("File Path Is : \(fileURL)")

return fileURL.path!
//returns the fileURL in path format?????
}

而且这里根本不需要这行代码。此代码还从应用程序的 DocumentDirectory 获取文件路径。这是在 getPath: 函数中完成的。

 let dirPaths =
    NSSearchPathForDirectoriesInDomains(.DocumentDirectory,
        .UserDomainMask, true)
    //creates search paths for directories, then ?????

    let docsDir = dirPaths[0] 

DocumentDirectory 是应用程序保存数据库的地方。 抱歉英语不好。希望对您有所帮助:)