Re: Why does initWithCoder has other self-object?
Re: Why does initWithCoder has other self-object?
- Subject: Re: Why does initWithCoder has other self-object?
- From: Peter <email@hidden>
- Date: Thu, 18 Aug 2011 09:39:32 +0200
You have two independent initializers in your class.
You should have one designated initializer which is called by all other initializers in your class.
Quoting from "The Objective-C Programming Language"
In the implementation of a custom initializer, you must ultimately invoke a designated initializer.
Designated initializers are described in “The Designated Initializer”; a full explanation of this issue is given in “Coordinating Classes.”
In brief, if you are implementing a new designated initializer, it must invoke the superclass’ designated initializer. If you are implementing any other initializer, it should invoke its own class’s designated initializer, or another of its own initializers that ultimately invokes the designated initializer.
This makes sure that you don't get inconsistencies like those you describe.
Am 17.08.2011 um 13:34 schrieb Arno Oesterheld:
> Hello,
>
> 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 mithin 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 differend 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?
>
> Any advice is very welcome.
>
> Thank you very much in advance!
>
> Best regards
> Arno
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
___ Peter Hartmann ________
mailto:email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden