macos - NSView拖动操作错误

标签 macos cocoa encoding drag-and-drop nsview

我在 OS X 上使用 Cocoa 进行编码。

我正在尝试接收一个被拖放到我的 NSView 子类上的文件 - 我可以这样做;并获取其内容和文件名并显示它们 - 我可以第一次对任何类型的文件执行此操作,但第二次,当我尝试拖动时另一个文件上,我只能设置标题setTitle:,但不能设置主体文本setText:

我收到的错误是:

Canceling drag because exception 'NSInternalInconsistencyException' (reason 'Invalid parameter not satisfying: aString != nil') was raised during a dragging session

Assertion failure in -[NSTextFieldCell _objectValue:forString:errorDescription:], /SourceCache/AppKit/AppKit-1187/AppKit.subproj/NSCell.m:1532

我的代码(抱歉,它很长!):

- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
NSPasteboard *pboard;
NSDragOperation sourceDragMask;

sourceDragMask = [sender draggingSourceOperationMask];
pboard = [sender draggingPasteboard];

if ([[pboard types] containsObject:NSFilenamesPboardType]) {

    NSURL *file = [NSURL URLFromPasteboard:pboard];
    //NSData *data = [NSData dataWithContentsOfURL:file];

    NSError *error;
    NSStringEncoding encoding;
    NSString *contentString = [[NSString alloc] initWithContentsOfURL:file usedEncoding:&encoding error:&error];

    NSLog(@"Error: %@",error);

    NSString *last = [[file path] lastPathComponent];
    NSArray *parts = [last componentsSeparatedByString:@"."];
    NSString *filename = [parts objectAtIndex:0];
    NSString *fileType = [parts objectAtIndex:1];

    NSLog(@"FILETYPE: %@", fileType);

    if ([fileType isEqualToString:@"txt"] || [fileType isEqualToString:@"md"]) {
        [self setTitle:filename];

        if (self.textViewString == (id)[NSNull null] || self.textViewString.length == 0) {
            [self setText:contentString];
        } else {
            BOOL whatToDo = (NSRunCriticalAlertPanel(@"What do you want to do?", nil, @"Append", @"Replace", nil) == NSAlertDefaultReturn);

            if (whatToDo) {
                //Append
                [self setText:[NSString stringWithFormat:@"%@\n%@",self.textViewString,contentString]];
            } else {
                //Replace
                [self setText:contentString];
            }
        }
        return YES;
    } else {
        return NO;
    }
} else if ([[pboard types] containsObject:NSPasteboardTypeString]) {
    NSString *draggedString = [pboard stringForType:NSPasteboardTypeString];

    if (self.textViewString == (id)[NSNull null] || self.textViewString.length == 0) {
        [self setText:draggedString];
    } else {
        [self setText:[NSString stringWithFormat:@"%@\n%@",self.textViewString,draggedString]];
    }
    return YES;
}
else {
    return NO;
}

}

提前致谢! :)

最佳答案

听起来 Cocoa 在引发任何异常时都会取消拖动,而当内部某些内容需要字符串并获取 nil 值时,就会引发异常。

这只是一个猜测,没有更多信息,但我预测 stringWithFormat: 会引发异常,因为它看起来是您所编写的内容中唯一真正可能脆弱的部分。

你做了一些不明智的事情。首先,您假设 -initWithContentsOfURL:usedEncoding:error: 成功。你不应该这样做。相反,您需要传递一个可以在错误时填写的 NSError ** ,测试 contentString 是否为 nil,如果是,则检查 error 相应地。我有一种感觉,您会发现您得到了nil,并且错误将解释原因。

可能不相关,但您的 if (whatToDo) 并没有按照您可能认为的那样进行。由于 whatToDo 是指向自动释放的 NSNumber 实例的指针,因此您的条件将始终评估为 true,因为指针是非零。您可能想要做的事情如下:

 BOOL whatToDo = (NSRunCriticalAlertPanel(@"What do you want to do?", nil, @"Append", @"Replace", nil) == NSAlertDefaultReturn);

    if (whatToDo) {
        //Append
        [self setText:[NSString stringWithFormat:@"%@\n%@",self.textViewString,contentString]];
    } else {
        //Replace

        [self setText:contentString];
    }

关于macos - NSView拖动操作错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11704540/

相关文章:

macos - quartz 2D锯齿线

iphone - Xcode4:为自定义核心数据管理对象生成不同的代码

python - 为什么以两种不同的编码打开文件可以按预期工作?

Android 媒体编解码器类型 "video/mp4v-es"- 是否与 MPEG-4 第 2 部分(MPEG-4 视觉效果)相同?

ruby - 如何将 unicode 字符串转换为其在 Ruby 中的符号字符?

macos - 如何在没有沙箱的情况下运行沙箱 OS X 应用程序?

linux - Mono 2.8中通过 "Package for Linux"创建包的问题

python - 带有 Eclipse 的 TensorFlow

macos - 如何跳过将可执行打印记录到 Mac 控制台

objective-c - OSX 中的事务性文件操作