2017-10-03 1 views
1

iOS 개발과 Swift에 완전히 새로운 기능입니다. 아래의 이미지처럼 파일의 목록을 보여줍니다 내 테이블보기의 편집 모드를 구현하려는 FileManager 변경 사항이 재정렬을 위해 작동하지 않고 ErrorDomain Code = 17 "File exists"가 표시됩니다.

:

how my table view displays

모든 편집 모드에서 잘 작동하고, 내 코드는 삭제에서 잘 작동하지만 파일을 드래그하여 파일 위치를 변경하면 매번 처음으로 파일이 다시 나타납니다. 파일 관리자 고원을 변경하는 코드가 실행되지 그리고이 오류가 있습니다

NSUnderlyingError=0x1c0249c90 {Error Domain=NSPOSIXErrorDomain Code=17 "File exists"}}`

이 줄을 호출 할 것이다 : 여기에서

do { 
     try FileManager.default.moveItem(atPath: (documentURL?.path)!, toPath: (documentURL_Dest?.path)!) 
    } 

을,이 뷰 컨트롤러 내 moveRowAt 기능은 다음과 같습니다

:
var documents = [PDFDocument]() 

    override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { 

    let movedObject = self.documents[sourceIndexPath.row] 

    let document = movedObject 
    let documentURL = document.documentURL 
    let document_Dest = documents[destinationIndexPath.row] 
    let documentURL_Dest = document_Dest.documentURL 




    documents.remove(at: sourceIndexPath.row) 

    documents.insert(movedObject, at: destinationIndexPath.row) 

    //this do wouldn't call and catch is calling each time 
    do { 


     try FileManager.default.moveItem(atPath: (documentURL?.path)!, toPath: (documentURL_Dest?.path)!) 


    } catch let error { 
     NSLog("Error in copying Data.plist: \(error)") // see the above quoted error message from here 
    } 


    refreshData() 

} 

여기 내 refreshData() 기능입니다 93,210

내가 viewDidLoad에서 편집 모드를 활성화 동안이 줄의 코드를 삽입 :

self.navigationItem.rightBarButtonItem = self.editButtonItem 

답변

0

을 오류가 자명 꽤 많이, FileManager는 폴더에 파일을 이동하지 않는 경우 의 파일 같은 이름가 이미 있습니다.

목록에서 파일 '을 변경하려면 파일을 다시로드하면 원래 순서가 재설정되므로 파일을 다시로드하지 않아야합니다.

당신이 당신의 파일에 하나 개 이상의 폴더를 보여주기 위해 계획되지 않거나 다른 위치로 파일을 이동하는 기능을 추가 할 수없는 경우, 당신은 당신의 코드의 전체 덩어리 제거해야합니다 :

do { 
    try FileManager.default.moveItem(atPath: (documentURL?.path)!, toPath: (documentURL_Dest?.path)!) 
} catch let error { 
    NSLog("Error in copying Data.plist: \(error)") // see the above quoted error message from here 
} 


refreshData() 

그렇지 않으면 해당 오류를 적절히 처리해야합니다 (예 : 해당 디렉터리에 같은 이름의 파일이 이미 있음을 사용자에게 알리는 경고 표시).

+0

도움 주셔서 대단히 감사합니다. thats 맞아요, 내 폴더에 두 파일의 사본이 필요하지 않습니다, 그냥 솔루션을 다시 정렬 문서가 제대로 작동하도록 찾을 필요가 내 문제는 내가 목록에서 내 항목의 장소를 변경하고 다음 응용 프로그램을 닫습니다 다시 실행하면 전체 변경 사항이 기본값으로 다시 설정됩니다. refreshData가 실행될 때마다 발생합니다. 어떤 솔루션이나 튜토리얼을 제안 해 주시겠습니까? 감사합니다 –

+0

'UserDefaults' 또는 어떤 종류의 데이터베이스 안에 주문을 저장해야합니다. 이에 대한 자세한 내용은 [여기] (https://www.hackingwithswift.com/example-code/system/how-to-save-user-settings-using-userdefaults)를 참조하십시오. – the4kman

+0

당신이 추천하는 페이지를 읽어 주셔서 대단히 감사합니다. 매우 유용하지만 신속하고 혼란 스럽습니다. 좀 더 도움이 필요해. 가능한 경우 NsUserDefaults의 일부 코드를 업데이트하고 알려주는 것이 가능합니다. 감사, –

관련 문제