게임 데이터를 저장하려고 할 때 오류가 발생했습니다. (Error when trying to save gamedata)


문제 설명

게임 데이터를 저장하려고 할 때 오류가 발생했습니다. (Error when trying to save gamedata)

게임 데이터를 저장하려고 합니다.

    if !fileManager.fileExistsAtPath(path) {
        // create an empty file if it doesn't exist
        if let bundle = NSBundle.mainBundle().pathForResource("DefaultFile", ofType: "plist") {
            fileManager.copyItemAtPath(bundle, toPath: path)
        }
    }

하지만 오류가 발생했습니다: 호출이 발생할 수 있지만 '시도'로 표시되지 않고 오류가 처리되지 않습니다.

해당 변종은 Swift에서 작동해야 하지만 Swift2에서는 작동하지 않습니다. 코드를 수정하는 방법은 무엇입니까?


참조 솔루션

방법 1:

You need to use Swifts new try catch

if (!fileManager.fileExistsAtPath(path)) {
    // create an empty file if it doesn't exist
    if let bundle = NSBundle.mainBundle().pathForResource("DefaultFile", ofType: "plist") {
        do {
            try fileManager.copyItemAtPath(bundle, toPath: path)
        } catch {
            //Catch Error Here
        }

    }
}

This should solve you problem, if not just write me :‑)

방법 2:

Th reason for the error

Call can throw, but it is not marked with 'try'

is because, the method

public func copyItemAtPath(srcPath: String, toPath dstPath: String) throws

indicates that the method that you are using can throw error(See the throws keyword?) so compiler instructs you to catch the error using swift try catch method

if !fileManager.fileExistsAtPath(path) {
// create an empty file if it doesn't exist
if let bundle = NSBundle.mainBundle().pathForResource("DefaultFile", ofType: "plist") {
             do  {
                try fileManager.copyItemAtPath(bundle, toPath: path)
             }
             catch {
                //Catch error here
             }
        }
    }

(by YaninaDennis Weidmannipraba)

참조 문서

  1. Error when trying to save gamedata (CC BY‑SA 2.5/3.0/4.0)

#swift2 #swift #iOS #plist






관련 질문

재정의된 메서드에 대한 신속한 문서 주석? (Swift documentation comments for overriden methods?)

Swift 2 "Type 'NSCalendarUint'에 'YearCalendarUnit' 멤버가 없습니다" 오류가 발생했습니까? (I got error with in Swift 2 "Type 'NSCalendarUint' has no member 'YearCalendarUnit' "?)

Xcode 7: 사용자 정의 키보드가 시뮬레이터에 표시되지 않음 (Xcode 7: Custom keyboard is not showing in simulator)

게임 데이터를 저장하려고 할 때 오류가 발생했습니다. (Error when trying to save gamedata)

Alamofire에서 캐시된 응답 감지 (Detect cached response in Alamofire)

언래핑 라인을 잡고 잡으시겠습니까? 스위프트 2.0, XCode 7 (Try and catch around unwrapping line? Swift 2.0, XCode 7)

ios swift tableview 셀 사이의 공간을 어떻게 변경할 수 있습니까? (How can I change the space between tableview cells ios swift)

.removeFromParent()가 작동하지 않음 (.removeFromParent() not working)

Swift2 UI 테스트 - 요소가 나타날 때까지 대기 (Swift2 UI Test - Wait for Element to Appear)

Swift의 View에 추가한 후 UIBezierPath의 색상을 변경하는 방법은 무엇입니까? (How to change the color of a UIBezierPath after added on View in Swift?)

swift에서 nil 객체와 문자열 연결 (string concatenate with nil object in swift)

포드 파일 없이 MWPhotoBrowser 사용 (Use MWPhotoBrowser without pod file)







코멘트