2016-08-23 2 views
0

저는 Swift와 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. 
} 


} 

답변

0

이 함수는 documentDirectory에서 fileName의 파일 경로를 가져 와서 다시 반환합니다.

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는 응용 프로그램이 데이터베이스를 저장하는 곳입니다. 나쁜 영어로 죄송합니다. 희망이 도움 :