我在 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/