开发者

Passing over Object between Classes not working

hey guys, i'm new to objective-c and i'm having trouble with the memory management. i declared 3 classes, Table, Dataset and my Main class. In my mainclass, i created an Dataset Object and now im trying to pass this Object over to a Tableobject, where i want to store it permanently. but it seems to me that the garbage collector kills the reference before i can use it.

heres some code:

Dataset:

    //Dataset.h
@interface Dataset : NSObject {
    NSMutableArray* daten; 
}

@end

//Dataset.m
#import "Dataset.h"
#import "Datensatz.h"

@implementation Dataset


- (id) init
{
    self=[super init];
    daten=[[NSMutableArray alloc] init];
    return self;
}

Table:

//Table.h

@class Dataset;

@interface Table : NSObject {
    Dataset* daten;
}
-(id)init:(NSTableView *)aTableView;
-(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex;
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView;
@property (retain) Dataset* daten;

@end

//Table.m

#import "Table.h"
#import "Dataset.h"
@impl

ementation Table

    @synthesize daten;

    -(id)init:(NSTableView*)aTableView
    {
        self=[super init];
        [self setDaten:[Datenmenge alloc]];
        return self;
    }
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
    NSLog(@"anzahl: %d %@",[self.daten anzahl], self.daten);//This is always 0 null
    return [daten anzahl];

}
    -(void)setDaten:(Dataset *)a
    {
        NSLog(@"setter: anzahl: %d %@",[a anzahl], a);
        [daten release];
        daten=[a retain];
        NSLog(@"setter: anzahl: %d %@",[daten anzahl], daten);
    }
    @end

In my mainclass i do the following:

  //init method
    [self setDaten:[[[Dataset alloc]init]autorelease]];
    tabelle=[[Table alloc] init:tableview];
    [tabelle setDaten:[self daten]];

Mainclass:

//code.h
//
//  MalWiederWasNeuesAppDelegate.h
//  MalWiederWasNeues
//
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <Cocoa/Cocoa.h>
@class Datenmenge,Graph,T开发者_如何学Cabelle;

@interface MalWiederWasNeuesAppDelegate : NSObject <NSApplicationDelegate> {
    NSWindow *window;
    NSToolbarItem *datenKnopf;
    NSToolbarItem *speichernKnopf;
    NSSlider *scaleSlider;
    NSScroller *moveSlider;
    NSTableView* tableview;
    Graph* graph;
    Tabelle* tabelle;
    Datenmenge* daten;

}

-(void)tuWas;

- (IBAction)datenHinzufuegen:(id)sender;
- (IBAction)speichern:(id)sender;

@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSToolbarItem *datenKnopf;
@property (assign) IBOutlet NSToolbarItem *speichernKnopf;
@property (assign) IBOutlet NSSlider *scaleSlider;
@property (assign) IBOutlet NSScroller *moveSlider;
@property (assign) IBOutlet Graph *graph;
@property (assign) IBOutlet Tabelle *tabelle;
@property (assign) IBOutlet NSTableView* tableview;
@property (retain) Datenmenge* daten;
@end

//code.m
//
//  MalWiederWasNeuesAppDelegate.m
//  MalWiederWasNeues
//
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MalWiederWasNeuesAppDelegate.h"
#import "Datenmenge.h"
#import "Graph.h"
#import "Tabelle.h"
@implementation MalWiederWasNeuesAppDelegate

@synthesize window;
@synthesize daten;
-(id) init
{
    self.daten=[[Datenmenge alloc]init];
    [self.daten datenHinzufuegen:nil];
    tabelle=[[Tabelle alloc] init:tableview];
    tabelle.daten=daten;

    NSLog(@"konstruktor: %f %d",[daten maximum],[daten anzahl]);
    //graph.daten=daten;

    return self;
}

-(void)tuWas{

}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application 
}

- (IBAction)datenHinzufuegen:(id)sender
{
    NSLog(@"%f %d",[daten maximum],[daten anzahl]);
    NSLog(@"daten hinzufügen");
}

- (IBAction) speichern:(id)sender
{
    NSLog(@"%@ %@",daten,[tabelle daten]);
    NSLog(@"speichern");    
}

@end

I hope this wasnt too much code for you. when i call a method of "tabelle", my Table object, "daten" does not refer to an Dataset Object. But the NSLogs in "setDaten" show me valid references. so, what am i doing wrong?

have a good evening, lukas


You define Daten as a retain type

@property (retain) Dataset* daten; and @synthesize daten;

theres no need to then implement the method

-(void)setDaten:(Dataset *)a thats what @synthesize daten; does

I think theres a lost in translation moment here so ill assume Table == Tabelle and Dataset == Datmenge and I dont see the implementation for your main class.

cast your eye over this too.

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html


Maybe, the daten property of your main class is declared as assign? In that case daten is correct when you call setDaten:, but might have been already autoreleased when you try to access it afterwards.

Also,

-(void)setDaten:(Dataset *)a
{
    NSLog(@"setter: anzahl: %d %@",[a anzahl], a);
    [daten release];
    daten=[a retain];
    NSLog(@"setter: anzahl: %d %@",[daten anzahl], daten);
}

is not a good implementation of a setter. If a == daten, then this object will be released (and maybe dealloc'd). You need to check identity of objects when implementing your own setter.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜