我正在嘗試使用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
是的。從gdb:`print(unsigned int)[data length]`返回`$ 1 = 271` – umop 2011-01-28 04:57:53