nsfilemanager

Download file from server using Swift

穿精又带淫゛_ 提交于 2019-11-26 23:16:47
问题 Hi I have a whole bunch of .mp3 files I want to use with NSFileManager and store in the documents folder. Is there a way I can download the .mp3 files online and then have it save to the documents folder? This is what I'm using for a local file. let filemanager = NSFileManager.defaultManager() let documentsPath : AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0] let destinationPath:NSString = documentsPath.stringByAppendingString("/Attention.mp3") if

Get directory contents in date modified order

五迷三道 提交于 2019-11-26 22:35:52
问题 Is there an method to get the contents of a folder in a particular order? I'd like an array of file attribute dictionaries (or just file names) ordered by date modified. Right now, I'm doing it this way: get an array with the file names get the attributes of each file store the file's path and modified date in a dictionary with the date as a key Next I have to output the dictionary in date order, but I wondered if there's an easier way? If not, is there a code snippet somewhere which will do

Array of multiple URL's NSFileManager Swift

廉价感情. 提交于 2019-11-26 21:45:50
问题 How would I go about adding multiple URL's? I wonder how I can set that up into an array with swift. if let audioUrl = NSURL(string: "http://freetone.org/ring/stan/iPhone_5-Alarm.mp3") { println("LOADING AUDIO") if let myAudioDataFromUrl = NSData(contentsOfURL: audioUrl){ println("AUDIO LOADED") let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL let destinationUrl = documentsUrl.URLByAppendingPathComponent(audioUrl

Get Subdirectories using Swift

送分小仙女□ 提交于 2019-11-26 21:41:47
问题 I'm trying to get ONLY subdirectories. Here's my data structure. root/ a/ 1.txt 2.txt b/ 3.txt 4.txt I want to get directories in a specific folder(root). I use enumerator in root file path: let enumerator = NSFileManager.defaultManager().enumeratorAtPath(filePath) for element in enumerator! { print(element) } However, It will iterate every files in root. How can I get just subdirectories (a&b) ? 回答1: There is no need to use a deep enumeration. Just get contentsOfDirectoryAtURL and filter the

Storing images locally on an iOS device

与世无争的帅哥 提交于 2019-11-26 21:33:04
Some iOS photo-related apps store images created with the app somewhere other than the photo library. For example Fat Booth shows a scrolling list of photos created with the app at app start-up. Note these photos are persisted w/o the user explicitly saving them to the photo library. What is the simplest way to save and persist images inside an iOS application? The only persistent stores I'm familiar with are NSUserDefaults and the key chain. However I've never heard of these used to store larger pieces of data such as an image. Now I'm wondering if Core Data is the easiest way. Tommy Devoy

How to overwrite a file with NSFileManager when copying?

两盒软妹~` 提交于 2019-11-26 18:03:32
问题 I'm using this method to copy a file: [fileManager copyItemAtPath:sourcePath toPath:targetPath error:&error]; I want to overwrite a file when it exists already. The default behavior of this method is to throw an exception/error "File Exists." when the file exists. There's no option to specify that it should overwrite. So what would be the safest way to do this? Would I first check if the file exists, then delete it, and then attempt to copy? This has the danger that the app or device goes OFF

Finding file's size

拥有回忆 提交于 2019-11-26 18:01:04
问题 In my iPhone app I am using the following code to find a file's size. Even though the file exists, I am seeing zero for the size. Can anyone help me? Thanks in advance. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *URL = [documentsDirectory stringByAppendingPathComponent:@"XML/Extras/Approval.xml"]; NSLog(@"URL:%@",URL); NSError *attributesError = nil; NSDictionary

NSFileManager creating folder (Cocoa error 513.)

家住魔仙堡 提交于 2019-11-26 15:47:20
问题 I'm trying to create a folder inside the /sounds folder of my app. -(void)productPurchased:(UAProduct*) product { NSLog(@"[StoreFrontDelegate] Purchased: %@ -- %@", product.productIdentifier, product.title); NSFileManager *manager = [NSFileManager defaultManager]; NSString *bundleRoot = [[NSBundle mainBundle] bundlePath]; NSError *error; NSString *dataPath = [NSString stringWithFormat:@"%@/sounds/%@", bundleRoot, product.title]; if (![manager fileExistsAtPath:dataPath isDirectory:YES]) {

Iterate through files in a folder and its subfolders using Swift's FileManager

假如想象 提交于 2019-11-26 12:54:18
问题 I\'m quite new to programming a Swift and I\'m trying to iterate through the files in a folder. I took a look at the answer here and tried to translate it to Swift syntax, but didn\'t succeed. let fileManager = NSFileManager.defaultManager() let enumerator:NSDirectoryEnumerator = fileManager.enumeratorAtPath(folderPath) for element in enumerator { //do something } the error I get is: Type \'NSDirectoryEnumerator\' does not conform to protocol \'SequenceType\' My aim is to look at all the

Get all folders of URL

纵饮孤独 提交于 2019-11-26 12:48:39
问题 Hi I\'m trying to read all files from a given server. What I want to do: Read all the folders Get the file URLs inside the folders I tried this to get the folders and filey of my server, but it returned me an array with the folders of my MacBook: NSURL *directory = [NSURL URLWithString:@\"linktoserver\"]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error = nil; self.contentList = [[NSArray alloc] initWithArray:[fileManager contentsOfDirectoryAtURL:directory