在类之间传递对象不起作用
嘿伙计们, 我是 Objective-C 的新手,并且在内存管理方面遇到了麻烦。我声明了 3 个类,表、数据集和我的主类。在我的主类中,我创建了一个数据集对象,现在我尝试将此对象传递给表对象,我想将其永久存储在其中。但在我看来,垃圾收集器在我可以使用它之前就杀死了它。
这是一些代码:
数据集:
//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.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
在我的主类中,我执行以下操作:
//init method
[self setDaten:[[[Dataset alloc]init]autorelease]];
tabelle=[[Table alloc] init:tableview];
[tabelle setDaten:[self daten]];
主类:
//code.h
//
// MalWiederWasNeuesAppDelegate.h
// MalWiederWasNeues
//
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@class Datenmenge,Graph,Tabelle;
@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
我希望这对您来说不是太多代码。 当我调用“tabelle”的方法时,我的表对象“daten”并不引用数据集对象。但“setDaten”中的 NSLogs 显示了有效的引用。 那么,我做错了什么?
祝你晚上愉快, 卢卡斯
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,Tabelle;
@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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将 Daten 定义为保留类型
@property (retain) Dataset* daten;
和@synthesize daten;
然后无需实现方法
-(void) setDaten:(Dataset *)a
这就是@synthesize daten;
我认为这里在翻译过程中丢失了所以我假设 Table == Tabelle 和 Dataset == Datmenge 我没有看到您的主类的实现。
也请关注这一点。
http://developer.apple.com/库/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html
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;
doesI 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
也许,你的主类的 daten 属性被声明为
assign
?在这种情况下,当您调用setDaten:
时,daten 是正确的,但当您稍后尝试访问它时,它可能已经被自动释放。而且,
这也不是一个很好的 setter 实现。如果 a == daten,则该对象将被释放(并且可能被释放)。在实现自己的 setter 时,您需要检查对象的身份。
Maybe, the daten property of your main class is declared as
assign
? In that case daten is correct when you callsetDaten:
, but might have been already autoreleased when you try to access it afterwards.Also,
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.