Save To A Created SubDirectory

Discussion in 'iOS Programming' started by newtoiphonesdk, Apr 19, 2012.

  1. newtoiphonesdk macrumors 6502a

    Joined:
    Jul 30, 2010
    #1
    In My appdelegate I run code to create a subdirectory in the Documents Folder:

    Code:
    NSFileManager *fileManager = [NSFileManager defaultManager];
    	
    	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    	NSString *pathToDocumentsDir = [paths objectAtIndex:0];
    	
    	NSString *subfolder = [pathToDocumentsDir stringByAppendingPathComponent:@"subfolder"];
        
    
        
        if (![fileManager fileExistsAtPath:subfolder isDirectory:&isDir] && isDir) {
            [fileManager createDirectoryAtPath:subfolder withIntermediateDirectories:NO attributes:nil error:nil];
    This creates the Folder just fine, however I am having issues when trying to write data into it.

    Code:
    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
        
        NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *subfolder = [documentsDirectory stringByAppendingPathComponent:@"subfolder"];
     
        
    
            NSString *tempPath = [[subfolder stringByAppendingString:thename] stringByAppendingFormat:@".mp4"];
            
            [videoData writeToFile:tempPath atomically:NO];
        
    
    The issue is that it ends up writing the file "Test" into the Documents Folder as "subfolderTest.mp4" Why is this not writing into the subdirectory?
     
  2. Carob macrumors newbie

    Joined:
    Apr 19, 2012
    #2
    Your subfolder variable doesn't end with a / (path separator), so instead use something like
    Code:
    NSString *tempPath = [subfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4", thename]]
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    Set a breakpoint on this line:
    Code:
            NSString *tempPath = [[subfolder stringByAppendingString:thename] stringByAppendingFormat:@".mp4"];
    
    Step thru, line by line, keeping an eye on the value of tempPath. Does it look correct? If not, what would correct it?

    If you haven't learned to use breakpoints (and why not?), the judicious use of NSLog() statements will also tell you what is really happening.

    You should learn how to find out what is really happening in your program, whether that's NSLog, or breakpoints and the debugger. It should be the first thing you do after seeing an unexpected result, not the last thing.
     

Share This Page