2016-09-24 2 views
1

스위프트 버전 작동하지 않음 3.스위프트 복사 -

나는 그것이 유통의 일환으로 그것을 포장 할 수 있도록 존재하지 않는 경우 시작시 내 응용 프로그램에 내 realm.default 파일을 복사하는 작업입니다

. 파일을 변경할 수 있어야 문서 디렉토리에 복사 할 수 있습니다.

불행히도 파일이 존재하지 않는다는 오류가 나타납니다. 경로가 올바른지, .app에 파일이 있는지 확인했습니다.

파일에 흰색 선이 그 위에 있습니다 (입력하지 마십시오). 열려고 시도 할 때이 종류의 Mac에서 열 수 없다고 말합니다. 쇼 패키지 내용을 선택하여 내용을 볼 수 있으며 그 안에 파일이 들어 있습니다. 다음

func applicationWillTerminate(_ application: UIApplication) { 
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
} 

func openRealm() { 

    let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL! 
    let bundleReamPath = Bundle.main.path(forResource: "default", ofType:"realm") 

    if !FileManager.default.fileExists(atPath: String(describing: defaultRealmPath)) { 
     do 
     { 
      try FileManager.default.copyItem(atPath: bundleReamPath!, toPath: String(describing: defaultRealmPath)) 
     } 
     catch let error as NSError { 
     // Catch fires here, with an NSError being thrown 
     print("error occurred, here are the details:\n \(error)") 
     } 
    } 
} 

오류 메시지입니다 (문맥 굵게) :

텍스트 ..

다음은 영역 파일을로드 내 응용 프로그램 위임의 코드입니다 오류가 발생했습니다. 자세한 내용은 다음과 같습니다. 오류 도메인 = NSCocoaErrorDomain 코드 = 4 "파일"default.realm "이 없습니다." UserInfo = {NSSourceFilePathErrorKey = /사용자/사용자 이름/라이브러리/개발자/코어 시뮬레이터/장치/4965D609-499A-4AE5-8ECC-3266DAE4BA87/데이터/컨테이너/번들/응용//default.realm, NSUserStringVariant = ( 복사 ), NSDestinationFilePath = file : /// 사용자/라이브러리/개발자/CoreSimulator/장치/4965D609-499A-4AE5-8ECC-3266DAE4BA87/데이터/컨테이너/데이터/응용 프로그램 /565716DD-E903-409F-B7C8-20154B0DF6BA/Documents/default.realm, NSFilePath =/사용자/사용자 이름/라이브러리/개발자/CoreSimulator/장치/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Bundle/Application/B324E72E-2B5A-4A02-BC2C-FB542FDA6957/AppName.app/default.realm, NSUnderlyingError = 0x618000241bc0 {오류 도메인 = NSPOSIXErrorDomain 코드 = 2 "No such file or directory"}}

누구든지 이에 대한 아이디어가 있습니다. 최소한 사용자/사용자 이름/라이브러리/개발자/CoreSimulator/장치/4965D609-499A-4AE5-8ECC-3266DAE4BA87/데이터/컨테이너/번들/응용 프로그램/B324E72E-2B5A-4A02-BC2C-FB542FDA6957까지의 경로 /AppName.app/가 있고 파일이 패키지 내에 포함되어 있습니다.

+0

아무에게도 아무 것도 없습니다. 불행히도 나는 아직도 붙어있다. – Apocal

답변

2

당신은 pathURL을 혼동하므로 Realm.Configuration.defaultConfiguration.fileURLURL의 인스턴스를 반환합니다. Bundle.main.path()String의 인스턴스를 반환합니다. URL의 문자열 표현은 path과 같지 않습니다.

예컨대

print(String(describing: defaultRealmPath)) 
// => file:///Users/.../Documents/default.realm 

print(defaultRealmPath.path) 
// => /Users/.../Documents/default.realm 

그래서 하나 (경로 또는 URL)를 사용해야합니다. 당신이 path를 사용하는 경우, 다음과 같은 defaultRealmPath.path 대신 String(describing: defaultRealmPath)를 사용

let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL! 
let bundleReamPath = Bundle.main.path(forResource: "default", ofType:"realm") 

if !FileManager.default.fileExists(atPath: defaultRealmPath.path) { 
    do 
    { 
     try FileManager.default.copyItem(atPath: bundleReamPath!, toPath: defaultRealmPath.path) 
    } 
    catch let error as NSError { 
     // Catch fires here, with an NSError being thrown 
     print("error occurred, here are the details:\n \(error)") 
    } 
} 

당신이 URL를 사용하는 경우, Bundle.main.url() 대신 Bundle main.path()을 : defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL을하자!

let bundleReamPath = Bundle.main.url(forResource: "default", withExtension:"realm")! 

if !FileManager.default.fileExists(atPath: defaultRealmPath.path) { 
    do 
    { 
     try FileManager.default.copyItem(at: bundleReamPath, to: defaultRealmPath) 
    } 
    catch let error as NSError { 
     // Catch fires here, with an NSError being thrown 
     print("error occurred, here are the details:\n \(error)") 
    } 
} 
+0

고마워요 키시 카와 – Apocal