views:

940

answers:

1

I have the following objective C class. It is to store information on a film for a cinema style setting, Title venue ID etc. Whenever I try to create an object of this class:

Film film = [[Film alloc] init];

i get the following errors: variable-sizedobject may not be initialized, statically allocated instance of Objective-C class "Film", statically allocated instance of Objective-C class "Film".

I am pretty new to Objective C and probably am still stuck in a C# mindset, can anyone tell me what I'm doing wrong?

Thanks michael

code:

// Film.m
#import "Film.h"
static NSUInteger currentID = -1;

@implementation Film

@synthesize filmID, title, venueID;

-(id)init {

    self = [super init];

    if(self != nil) {
        if (currentID == -1)
        {
            currentID = 1;
        }
        filmID = currentID;
        currentID++;
        title = [[NSString alloc] init];
        venueID = 0;
    }

    return self;
}

-(void)dealloc {

    [title release];
    [super dealloc];
}

+(NSUInteger)getCurrentID {
    return currentID;
}

+(void)setCurrentID:(NSUInteger)value {
    if (currentID != value) {
        currentID = value;
    }
}

@end

// Film.h
#import <Foundation/Foundation.h>


@interface Film : NSObject {
    NSUInteger filmID;
    NSString *title;
    NSUInteger venueID;
}

+ (NSUInteger)getCurrentID;
+ (void)setCurrentID:(NSUInteger)value;

@property (nonatomic) NSUInteger filmID;
@property (nonatomic, copy) NSString *title;
@property (nonatomic) NSUInteger venueID;

//Initializers
-(id)init;

@end
+3  A: 

You need your variable that holds the reference to your object to be of a reference type. You do this by using an asterisk - see below:

Film *film = [[Film alloc] init];

Coming from Java I often think of the above as:

Film* film = [[Film alloc] init];

I tend to associate the 'reference' marker with the type. But hopefully someone more versed in C/C++/ObjC will tell me why this is wrong, and what the 'asterisk' is actually called in this context.

teabot
Thank you you genius. I still don't fully understand pointers, memory management is handled for you in c#.
e.James
Thanks for the information @eJames
teabot