gpt4 book ai didi

ios - Swift FMDB 代码说明

转载 作者:行者123 更新时间:2023-11-30 13:08:04 30 4
gpt4 key购买 nike

我是 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com