1

この Apple ウォークスルーを使用してドキュメント ベースの 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
4

2 に答える 2

0

1つの例外を除いて、プロジェクトを最初から再構築しました。MyDocumentクラスをファイルの所有者にドラッグする手順に従わなかったということです。チュートリアルは、以前のバージョンのXCode用に作成されていますが、3.2用であると書かれています(または、そのバージョンと現在でそれだけのことが起こっている可能性があります)が、その手順は不要です。

于 2011-02-05T02:53:02.853 に答える
0

あなたの-dataOfType:error:メソッドでは、何かを に代入するとき、dataそれが nil ではないことを確信していますか? nil を返すと、このエラーが発生します。

于 2011-01-24T16:52:08.010 に答える