Cocoa – NSFileManager removeItemAtPath Not Working
I am trying to delete a file, but somehow nsfilemanager will not allow me to do so. I do use the file in one line of code, but once that action has been ran, I want the file deleted. I have logged the error code and message and I get error code: 4 and the message:
"text.txt" could not be removed
Is there a way to fix this error “cleanly” (without any hacks) so that apple will accept this app onto their Mac App Store?
This is what I am using:
[[NSFileManager defaultManager] removeItemAtPath:filePath error:NULL];
- Why are Objective-C delegates usually given the property assign instead of retain?
- Fetched Property in XCode Data Model Editor for minimum value
- Get the count of files in a directory
- Better way of incrementing build number?
- Detect UITextView scroll location
- Sorting with sortedArrayUsingDescriptors and Key Paths
5 Solutions Collect From Internet About “Cocoa – NSFileManager removeItemAtPath Not Working”
Error code 4 seems to be NSNoSuchFileError. If the file you want to delete really exists, then you have got the path wrong. You’ll need to post some code if you want us to tell you exactly how you got the path wrong.
If the file doesn’t exist, you can ignore the error.
I had a similar problem in swift.For some reason fileManager.removeItemAtPath did’t work and I changed fileManager.removeItemAtPath(filePath) to fileManager.removeItemAtURL(fileURL) and it works fine.
let fileManager = NSFileManager() let documentsFolderUrl = fileManager.URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false, error: nil) let soundURL = documentsFolderUrl!.URLByAppendingPathComponent(recording.path) let stringTrimmedFilePath = "trimmed_\(recording.path)" let trimmedSoundURL = documentsFolderUrl!.URLByAppendingPathComponent(stringTrimmedFilePath) var error: NSError? fileManager.removeItemAtURL(trimmedSoundURL, error: &error)
First you need to pick the path for the document directory then you can delete the file.
Only remove statement is not sufficient.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectoryPath = [paths objectAtIndex:0]; NSString *databaseFile = [documentsDirectoryPath stringByAppendingPathComponent:@"text.txt"]; NSFileManager *fileManager = [NSFileManager defaultManager]; [fileManager removeItemAtPath:databaseFile error:NULL];
use this for solving your problem.
Your path is incorrect
Use the following
NSString *str = [outputFieldURL path];
NSString *str = [outputFieldURL absoluteString];
The method “removeItemAtPath:” need the local path of file, If you want to remove using url, you should use
I just figure it out of something very important when you use NSFileManager. You have to be aware of App Sandboxing.
let documentDirectory = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true)
This line return the path document directory of your app sandboxed. When you create a file with FileManager in your document directory (for e.g) don’t save the full file path but only the path from the current document directory.
You’ll be able to recreate the full path of your created file.
Hope (after 5 years) help over developers 😉
- How to put the UIPageControl element on top of the sliding pages within a UIPageViewController?
- How to “select a team” when Xcode thinks I'm not a registered iOS developer? Also, do I need an iPhone to get a provisioning profile?
- How to achieve Equilizer Setting for iPhone in Audio Player
- NSTextField, Change text in Swift
- How to migrate Alamofire router class to Swift 3?
- How can i include <bits/stdc++> in xcode
- xCode 7 error: include of non-modular header inside framework module with Google Maps
- Multiple Delegates in iOS
- iOS8.1 Simulator always uses US keyboard layout despite german hardware keyboard
- How safe is information contained within iPhone app compiled code?
- How to apply custom styling to NSTableHeaderView?
- If statement checking for NSString equality using ==
- Add button on top of UITableViewController (Swift)
- iOS In App Purchase: test a real purchase without submitting to Apple
- Global Variable / Constant in React Native