2016-03-24 13 views
0

Yorgunum bu sorunu çözmek için ama ben .lastPathComponent enter image description hereCoreData swift'de fotoğrafı silmek için prepareForDeletion nasıl kullanılır?

override func prepareForDeletion() { 

    super.prepareForDeletion() 
    if filePath != nil { 

     let fileName = (filePath! as NSString).lastPathComponent 

     let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentationDirectory, .UserDomainMask, true)[0] 

     let pathArray = [dirPath, fileName] 

     let fileURL = NSURL.fileURLWithPathComponents(pathArray)! 

     do { 
      try NSFileManager.defaultManager().removeItemAtURL(fileURL) 
      try NSFileManager.defaultManager().removeItemAtPath(fileName) 
     } catch let error as NSError { 
      print("Error from prepareForDeletion - \(error)") 
     } 
    } else { 
     print("filepath is empty") 
    } 
} 

Doğru filePath doğru cevap

olduğunu tespit

got kullanmak çünkü diğer fotoğraf yolunu kaldırmak kaldırdığınızda

override func prepareForDeletion() { 
    super.prepareForDeletion() 

    if filePath != nil { 

    if let imagePath = self.filePath { 

    do { 
     try NSFileManager.defaultManager().removeItemAtPath(imagePath) 
    } catch let error as NSError { 
     print("Error from prepareForDeletion - \(error)") 
    } 
    } 
} else { 
print("filepath is empty") 
    } 
    } 

cevap

0

Neden "do" ifadesinde iki "kaldırma" ifadesi kullanıyorsunuz? Lütfen

numaralı satırı kaldırın ve hatanın hala mevcut olup olmadığını kontrol edin.

+0

i içeri mağazayı olmamalı sınıf Foto –

0

Dosya yolunu veritabanına kaydetme, sadece dosya adını veritabanına kaydet, ardından silme işlevinde yol oluştur. Bu yöntemi deneyin.

override func prepareForDeletion() { 

     super.prepareForDeletion() 
     if filename != nil { 
    let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentationDirectory, .UserDomainMask, true)[0] 

     let pathArray = [dirPath, filename] 

     let fileURL = NSURL.fileURLWithPathComponents(pathArray)! 

     do { 
      try NSFileManager.defaultManager().removeItemAtURL(fileURL) 
     } catch let error as NSError { 
     print("Error from prepareForDeletion - \(error)") 
     } 
     } else { 
       print("filepath is empty") 
     } 

}

+0

cevap elde etmeye çalışmak iken ben eklendi çıkarmak veri tabanı. Yapmanız gereken, dosya adını filePath özelliğine kaydetmektir. mülk adını değiştirmeyi planlamadığınız için. daha sonra paylaşılan işlevini çağırın –

+0

FilePath nasıl filePath olan –

+0

dosya yolundan bir özelliktir o bunu – Saood

İlgili konular