2011-01-24 5 views
1

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 

답변

0

한 가지 예외를 제외하고 프로젝트를 처음부터 다시 작성했습니다. MyDocument 클래스를 File 's Owner로 드래그하는 단계를 수행하지 않았습니다. 이 튜토리얼은 XCode의 이전 버전에 대해 작성되었지만 3.2 버전 (또는 그 버전에서 지금까지 많이 발생 했음)이지만이 단계는 필요하지 않습니다.

0

, 당신이 그것을 확실 전무 아니에요입니까? nil을 리턴하면이 오류가 발생합니다.

+0

예. gdb :'print (unsigned int) [데이터 길이]''$ 1 = 271'을 반환합니다. – umop