Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
838 views
in Technique[技术] by (71.8m points)

iphone - Creating Linked Lists in Objective C

typedef struct {
    NSString *activty;
    NSString *place;
    float latitude;
    float longitude;
} event;

typedef struct {
    event *thing;
    Node *next;
} Node;

This is the code I have in my .h file to create two structs to hold data (one for an events name/place/location, and one for the nodes of a linked list. I can use the event struct in the node struct, but I need to use the node struct within itself. In C++ this would work, but how can I achieve this in objective-c? Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Why bother with a struct? Just use a class:

@interface MyEvent:NSObject
@property(copy) NSString *activity;
@property float latitude;
... etc ...
// and that linked list gunk
@property(retain) MyEvent *nextEvent;
@end

@implementation MyEvent
@synthesize activity, latitude, nextEvent;

- (void) dealloc
{
    [activity release], activity = nil;
    [nextEvent release], nextEvent = nil;
    [super dealloc];
}
@end

There is no significant overhead vs. a structure (if the method calls are really measurable, you could even expose ivars directly). Better yet, the moment you want to archive the struct, add business logic, or do anything else interesting, you can simply add methods.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...