small medium large xlarge

Back to: All Forums  Core Data
Code_monkey_pragsmall
23 Jan 2010, 15:57
Fernando López (6 posts)

Hello, in reference to page 25, the filename and applicationSupportFolder method seems to have disappeared from Mac OS X 10.6. I am attaching my solution with new methods:

- (void)addImageSheetDidEnd:(NSOpenPanel*) openPanel returnCode:(NSInteger) returnCode contextInfo:(NSManagedObject*)recipe { if (returnCode == NSCancelButton) return; NSString* path = [[openPanel filenames] objectAtIndex:0]; NSString* dest_path = [self applicationSupportDirectory]; NSString* guid = [[NSProcessInfo processInfo] globallyUniqueString]; dest_path = [dest_path stringByAppendingPathComponent:guid]; NSError* error = nil; [[NSFileManager defaultManager] copyItemAtPath:path toPath:dest_path error:&error]; if (error != nil) { [NSApp presentError:error]; } [recipe setValue:dest_path forKey:@"imagePath"]; }

Avatarsmall_pragsmall
24 Jan 2010, 17:04
Marcus S. Zarra (284 posts)

First, a reposting of your code for clarity:

- (void)addImageSheetDidEnd:(NSOpenPanel*)openPanel 
                 returnCode:(NSInteger)returnCode 
                contextInfo:(NSManagedObject*)recipe 
{ 
  if (returnCode == NSCancelButton) return; 
  NSString* path = [[openPanel filenames] objectAtIndex:0]; 
  NSString* dest_path = [self applicationSupportDirectory]; 
  NSString* guid = [[NSProcessInfo processInfo] globallyUniqueString]; 
  dest_path = [dest_path stringByAppendingPathComponent:guid]; 
  NSError* error = nil; 
  [[NSFileManager defaultManager] copyItemAtPath:path toPath:dest_path error:&error]; 
  if (error != nil) { 
    [NSApp presentError:error]; 
  } 
  [recipe setValue:dest_path forKey:@"imagePath"]; 
}

The method -filename is still available (although deprecated) in 10.6. It is part of the NSOpenPanel which NSSavePanel is a subclass of. However since even -filenames is deprecated in 10.6, this code will be updated in the next release to use URLs instead.

As for -applicationSupportFolder that is the default name used by 10.5. It has not disappeared but renamed based on some new naming schema in 10.6. The code inside of -applicationSupportDirectory is the same.

Generic-user-small
14 Aug 2011, 00:28
SC (15 posts)

In 10.7 OS X Lion and XCODE 4.1 there seems to be more changes. This is what I did. I haven’t tested this code out yet…

- (IBAction)addImage:(id)sender
{
    id recipe = [[recipeArrayController selectedObjects] lastObject];
    if (!recipe) return;
    
    NSOpenPanel *openPanel = [NSOpenPanel openPanel];
    
    [openPanel setCanCreateDirectories:NO];
    [openPanel setCanCreateDirectories:NO];
    [openPanel setAllowsMultipleSelection:NO];
        
    void (^addImageHandler)(NSInteger) = ^( NSInteger result )
    {
        if (result == NSFileHandlingPanelCancelButton) return;
        
        NSURL *imageDirectoryURL = [openPanel URL];
        
        if (imageDirectoryURL) {
            NSString *imageDirectoryPath = [imageDirectoryURL path];
            
            if (imageDirectoryPath) {
                [imageDirectoryPath retain];
                
                NSString *guid = [[NSProcessInfo processInfo] globallyUniqueString];
                NSURL *destinationURL = [self applicationFilesDirectory];
                destinationURL = [destinationURL URLByAppendingPathComponent:guid];

                NSError *error = nil;

                [[NSFileManager defaultManager] copyItemAtURL:imageDirectoryURL toURL:destinationURL error:&error];
            } // if
        } // if
    }; // void
    
    
    [openPanel beginSheetModalForWindow:window
                      completionHandler:addImageHandler];
}
Generic-user-small
11 Jan 2012, 06:46
Jeremy Lunn (1 post)

In keeping with the solution in the book, I found the following to work but haven’t tested it (though it compiles):

- (void)addImageSheetDidEnd:(NSOpenPanel*)openPanel
returnCode:(NSInteger)returnCode
contextInfo:(NSManagedObject*)recipe
{
    if (returnCode == NSCancelButton) return;
    NSURL *path = [openPanel URL];
    
    // Build the path we want the file to be at
    NSURL *destPath = [self applicationFilesDirectory];
    NSString *guid = [[NSProcessInfo processInfo] globallyUniqueString];
    destPath = [destPath URLByAppendingPathComponent:guid];
    NSError *error = nil;
    [[NSFileManager defaultManager] copyItemAtURL:path 
                                            toURL:destPath 
                                             error:&error];
    
    if (error){
        [NSApp presentError:error];
    }
    [recipe setValue:destPath forKey:@"imagePath"];
}

applicationFilesDirectory now returns an USURL instead of NSString. The other paths expressed as NSString are also changed to NSURL.

You also need to synthesise recipeArrayController at the beginning of AppDelegate.m as below: @synthesize recipeArrayController = _recipeArrayController;

Then update the following line in addImage to use the accessor via self: id recipe = [[self.recipeArrayController selectedObjects] lastObject];

You must be logged in to comment