gpt4 book ai didi

objective-c - 文档 “Untitled” 无法另存为 "Untitled"

转载 作者:行者123 更新时间:2023-12-03 17:26:01 25 4
gpt4 key购买 nike

我正在尝试使用this Apple walkthrough开发一个基于文档的Mac应用程序我在保存文件时遇到问题(最后一步)。我尝试保存文件后遇到的错误是:文档“无标题”无法保存为“- 新文件名是我正在尝试使用 -”

我用谷歌搜索了一下,没有找到这个错误的任何结果。我重新检查了代码,本教程中的所有内容似乎都非常可靠。我想知道是否有人对这里可能出了什么问题有任何直觉。

我的主类的代码是:

#import "MyDocument.h"

@implementation MyDocument

- (id)init
{
self = [super init];
if (self) {
if (mString == nil) {
mString = [[NSAttributedString alloc] initWithString:@""];
}
}
return self;
}

- (NSAttributedString *) string { return [[mString retain] autorelease]; }

- (void) setString: (NSAttributedString *) newValue {
if (mString != newValue) {
if (mString) [mString release];
mString = [newValue copy];
}
}

- (void) textDidChange: (NSNotification *)notification {
[self setString: [textView textStorage]];
}



- (NSString *)windowNibName
{
// Override returning the nib file name of the document
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
return @"MyDocument";
}

- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];

if ([self string] != nil) {
[[textView textStorage] setAttributedString: [self string]];
}
}

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
BOOL readSuccess = NO;
NSAttributedString *fileContents = [[NSAttributedString alloc]
initWithData:data options:NULL documentAttributes:NULL
error:outError];
if (fileContents) {
readSuccess = YES;
[self setString:fileContents];
[fileContents release];
}
return readSuccess;
}

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
NSData *data;
[self setString:[textView textStorage]];
NSMutableDictionary *dict = [NSDictionary dictionaryWithObject:NSRTFTextDocumentType
forKey:NSDocumentTypeDocumentAttribute];
[textView breakUndoCoalescing];
data = [[self string] dataFromRange:NSMakeRange(0, [[self string] length])
documentAttributes:dict error:outError];
return data;
}

头文件:

#import <Cocoa/Cocoa.h>

@interface MyDocument : NSDocument
{
IBOutlet NSTextView *textView;
NSAttributedString *mString;
}

- (NSAttributedString *)string;
- (void) setString: (NSAttributedString *)value;

@end

最佳答案

在您的 -dataOfType:error: 方法中,当您将某些内容分配给 data 时,您确定它不是 nil 吗?返回 nil 将导致此错误。

关于objective-c - 文档 “Untitled” 无法另存为 "Untitled",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4784296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com