开发者

Creating a custom class with primitive data types Obj-c

I'm trying to make a custom class to hold data in my iPhone app. I want a box object that will hold 2 variables, it's state (as an int) and whether it's filled or not. (as a bool) In my .h I have this:

#import <Foundation/Foundation.h>

@interface box : NSObject
{
    BOOL filled;
    int state;
}

- (id)init;
-(void)setState:(int)thestate;
-(BOOL)isFilled;

-(BOOL)filled;
-(int)state;
-(void)setFilled:(BOOL)input;
-(void)setState:(int)input;
@end

and in my .m I have this:

#import "box.h"

@implementation box

- (id)init
{开发者_高级运维
    self = [super init];
    if (self) {
        [self setState:0];
        [self setFilled:NO];
    }

    return self;
}

-(void) setState:(int)input
{
    state = input;
    [self setFilled:YES];
    if (state == 0)
    {
        [self setFilled:NO];
    }
}

-(int) getState
{
    return self.state;
}

-(BOOL)filled
{
    return self.filled;
}

-(void)setFilled:(BOOL)input
{
    filled = input;
}
@end

I'm making my own setters and getters because when I did @property (nonatomic) int state; I had problems. Whenever getState is called, I get a Program received signal: "SIGABRT" error. The console says:

-[box state]: unrecognized selector sent to instance 0x4e51970
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[box state]: unrecognized selector sent to instance 0x4e51970'

How do I fix this error? Thanks.


Were you synthesizing the properties using @synthesize in your class implementation?

EDIT When a property is synthesized, by default the getter will be propname and not getPropname.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜