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;
}
헤더 파일 : 당신이 data
에 뭔가를 할당 할 때
-dataOfType:error:
방법에서
#import <Cocoa/Cocoa.h>
@interface MyDocument : NSDocument
{
IBOutlet NSTextView *textView;
NSAttributedString *mString;
}
- (NSAttributedString *)string;
- (void) setString: (NSAttributedString *)value;
@end
예. gdb :'print (unsigned int) [데이터 길이]''$ 1 = 271'을 반환합니다. – umop