2

我正在写入沙盒应用程序中的文件,然后尝试使用-[NSURL setResourceValue:theTags forKey:NSURLTagNamesKey error:&theTaggingError];. 我没有收到任何错误(即标签成功应用一次),但最终文件被替换并且标签丢失。这仅在沙盒应用中;如果我关闭沙盒,一切正常。在沙盒模式下,如果我在不写入文件的情况下设置标签 - 再次工作正常。

本质上,我无法在写入文件后为其设置标签。我该如何解决?有什么见解吗?

示例代码:

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
    NSWindow *_window;
    NSURL *_saveURL;
    NSSavePanel *_savePanel;
}
@property (assign) IBOutlet NSWindow *window;
@property (retain) NSURL *saveURL;
@property (retain) NSSavePanel *savePanel;
- (IBAction)writeAndTag:(id)sender;
- (IBAction)justTag:(id)sender;
@end


@implementation AppDelegate

@synthesize window = _window;
@synthesize saveURL = _saveURL;
@synthesize savePanel = _savePanel;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    self.savePanel = [NSSavePanel savePanel];
}

- (IBAction)writeAndTag:(id)sender
{
    [self.savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
        self.saveURL = [self.savePanel URL];

        NSString *testString = @"Hello!";
        NSError *error = nil;
        [testString writeToFile:[self.saveURL path] atomically:NO encoding:NSUTF8StringEncoding error:&error];
        if(error)
        {
            NSLog(@"Err in saving: %@" ,error);
            error = nil;
        }
        // Tag is lost here
        BOOL success = [self.saveURL setResourceValue:[NSArray arrayWithObjects:@"Test", nil] forKey:NSURLTagNamesKey error:&error];
        NSLog(@"Tagging success: %@", (success)?@"YES":@"NO");
        if(error)
        {
            NSLog(@"Err in tagging: %@" ,error);
        }
    }];
}

- (IBAction)justTag:(id)sender
{
    // Works fine
    [self.saveURL setResourceValue:[NSArray arrayWithObjects:@"Test", nil] forKey:NSURLTagNamesKey error:NULL];
}

@end
4

0 回答 0