ios - Swift FMDB 代码说明

标签 ios swift syntax fmdb

我是 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 获取给定文件名的文件路径并将其返回。

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 是应用程序保存数据库的位置。 抱歉英语不好。希望它有帮助:)

关于ios - Swift FMDB 代码说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39099667/

相关文章:

ios - 从推送通知启动时,launchOptions 始终为零

arrays - 访问数组中对象的方法。 ( swift )

swift - 使用关联类型的 Swift 中的协议(protocol)一致性问题

bash - 为什么元字符周围有时需要空格?

linux - "Linux"Shell 中的引号中的引号中的引号

ios - 当 iPhone 连接到配件时,有没有办法访问当前播放的轨道?

ios - RestKit session 管理

ios - 即使多个 subview Controller 的 View 不可见,是否可以将多个 subview Controller 与父 UIViewController 关联起来?

ios - 如何在textview中获取滚动位置? ( swift )

python - 通过属性以及索引访问递归访问字典?