2016-12-22 2 views
0

Xcode의 놀이터를 통해 일부 sqlite 데이터베이스 호출을 테스트하려고합니다. 내 놀이터의 Resources 폴더에있는 데이터베이스로 시작하여 그것을 Playgrounds Documents 폴더로 옮기려고하지만, 그런 일이 일어나면 Resources 폴더 내의 파일을 가리키는 심볼릭 링크가 생성되어 그 파일에 쓸 수 없게된다. 그러나 Documents 폴더가 어디에 있는지 파악한 다음 터미널에서 손으로 파일을 복사하면 모든 것이 올바르게 작동합니다.xCode 놀이터 및 문서에 파일 쓰기

그렇다면 파일 관리자 복사 명령이 실제로 복사하는 대신 sym 링크를 실제로 만드는 이유는 무엇입니까? 실제로 이런 일이 일어날 수있는 방법이 있습니까? 놀이터에서만 문제가되는 것 같습니다. 리소스에서 문서로 복사가 앱 자체에서 올바르게 작동합니다.

놀이터에서 테스트하는 몇 가지 코드 ...

let dirPaths =  NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true) 
let docsDir = dirPaths[0] 
let destPath = (docsDir as NSString).appendingPathComponent("/data.sqlite") 
print(destPath) 

let fileMgr = FileManager.default 


let srcPath = Bundle.main.path(forResource: "data", ofType:"sqlite") 
// This copies the data.sqlite file from Resources to Documents 
// ** However in the playground, only a symlink is generated 
do { 
    try fileMgr.copyItem(atPath: srcPath!, toPath: destPath) 
} catch let error { 
    print("Error (during copy): \(error.localizedDescription)") 
} 

답변

1

로드는, 내가이 알아낼, 너무 늦기하지만.

상황 :

  • 내가
  • 내가 Store.db 추가 프레임 워크를 생성, 놀이터 내 프로젝트와 함께 작동하려면 동일한 작업 공간
  • 내 프로젝트에 하나의 놀이터를 추가 파일을 프레임 워크 대상에 전송
  • 놀이터에 Store.db를 자원으로 추가하지 않았습니다.
  • 스위프트 3

놀이터

@testable import MyAppAsFramework 

func copyMockDBToDocumentsFolder(dbPath: String) { 
    let localDBName = "Store.db" 
    let documentPath = dbPath/localDBName 
    let dbFile = Bundle(for: MyAppAsFrameworkSomeClass.self).path(forResource: localDBName.deletingPathExtension, ofType: localDBName.pathExtension) 

    FileManager.copyFile(dbFile, toFolderPath: documentPath) 
} 

copyMockDBToDocumentsFolder(dbPath: "The documents path") 

/ copyFile(x) 같은 것들과 다른 운영자 과부하 및 확장

extension String { 

    public var pathExtension: String { 
     get { 
      return (self as NSString).pathExtension 
     } 
    } 

    public var deletingPathExtension: String { 
     get { 
      return (self as NSString).deletingPathExtension 
     } 
    } 

    public func appendingPath(_ path: String) -> String { 
     let nsSt = self as NSString 
     return nsSt.appendingPathComponent(path) 
    } 

} 


infix operator/: MultiplicationPrecedence 

public func/(left: String, right: String) -> String { 
    return left.appendingPath(right) 
} 


extension FileManager { 

    class func copyFile(_ filePath: String?, toFolderPath: String?) -> Bool { 

     guard let file = filePath else { return false } 
     guard let toFolder = toFolderPath else { return false } 

     var posibleError: NSError? 

     do { 
      try FileManager.default.copyItem(atPath: file, toPath:toFolder) 
     } catch let error as NSError { 
      posibleError = error 
      print("CAN'T COPY \(error.localizedDescription)") 
     } 

     return posibleError == nil 
    } 

} 
있습니다