开发者

Why does initWithCoder has other self-object?

开发者 https://www.devze.com 2023-03-28 23:55 出处:网络
I am a beginner with Xcode and Objective-C and stuck with a quite simple thing for over two days now. I hope you can help me.

I am a beginner with Xcode and Objective-C and stuck with a quite simple thing for over two days now. I hope you can help me.

My Project is deployed for OS X 10.6, it uses Garbage Collection and I am using Xcode 4.0.1.

I made a multi document application starting with the template provided by Xcode. I just have one class as subclass of NSDocument.

For opening documents I use initWithCoder:. The decoding within this method works fine - I get the values that were saved. But these values are "lost" when I would like to use them in an other method (of the same class).

I assume that I make some mistakes with using the right comibation of init: initWithCoder:, initWithContentsOfURL: etc. The self-object does always have a different adress in the initWithCoder: method then in the other methods. I tried plenty of combinations of the above methods and even tried to call different methods in the super class (NSDocument) within initWithCoder:.

This is my header file:

#import <Cocoa/Cocoa.h>

@interface OptimiererZwoMultiDoc : NSDocument <NSCoding> {
    __strong struct bildanalyse { 
        float winkelo;
        ...
        float skalfak;    // Der Skalierungsfaktor, den dieses Bild erfahren muss damit es so gross ist wie das kleinste - Wert ist also immer <= 0
    };

    __strong struct bildanalyse *analyse;
    __strong int16_t anzahlanalysewerte;
    ...

@private
    NSTextView *ausgabe;
    ...
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification;
- (void) prepareAnalyseDoc;
...

@property (assign) IBOutlet NSTextView *ausgabe;
@property __strong struct bildanalyse *analyse;
@property __strong int16_t anzahlanalysewerte;
@end

When I try this implementation:

#import "OptimiererZwoMultiDoc.h"

@implementation OptimiererZwoMultiDoc

@synthesize ausgabe;
@synthesize analyse;
@synthesize anzahlanalysewerte;
...


- (id)init
{
    self = [super init];
    NSLog(@"init self=%@",self);
    if (self) {
        ...
    }
    return self;
}


- (NSString *)windowNibName
{
    NSLog(@"windowNibName self=%@",self);
    return @"OptimiererZwoMultiDoc";
}


- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
    NSLog(@"windowControllerDidLoadNib self=%@",self);
    [super windowControllerDidLoadNib:aController];
}


- (BOOL) readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError{
     NSLog(@"readFromData self=%@",self);
     [NSKeyedUnarchiver unarchiveObjectWithData: data];
     if (outError) {
         *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
     }
     return YES;
}


- (id) initWithCoder: (NSCoder *) coder{
    struct bildanalyse tempAnalyse;

    NSLog(@"initWithCoder self=%@",self);
    anzahlanalysewerte = [coder decodeIntForKey:@"anzahlanalysewerte"];
    ....
    return self;
}

then I get this output:

init self= OptimiererZwoMultiDoc: 0x2002955a0

readFromData self= OptimiererZwoMultiDoc: 0x2002955a0

initWithCoder self= OptimiererZwoMultiDoc: 0x20028f5e0

windowNibName self= OptimiererZwoMultiDoc: 0x2002955a0

windowControllerDidLoadNib self= OptimiererZwoMultiDoc: 0x2002955a0

As you can see, the object self is different in initWithCoder:. Why? What's wrong with 开发者_如何学编程my code?


Your -initWithCoder: is missing a self = [super initWithCoder:coder];. I’m not sure why your -init and -initWithCoder: are both being called—an object that’s getting unarchived from a NIB should just be receiving the latter—but the above would be a good place to start.


It's because (provided the document info in your Info.plist is set up correctly) Cocoa's document-handling machinery automatically instantiates your NSDocument subclass for you. So if you're using -initWithCoder: to manually create your own subclass instance, chances are that one is completely superfluous. I'd suggest reading through Apple's Document-Based Applications Overview before going much further.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号