Error: Accessing unknown getter method?
Thanks...
#import <Foundation/Foundation.h>
@interface puppy : NSObject {
int mack;
int jack;
}
-(puppy *) waldo: (puppy *) f;
-(void) setMack: (int) m;
-(void) setJack: (int) j;
@end
///////////////////////////////////////////////////
#import "puppy.h"
@implementation puppy
-(void) setJack: (int) j{
jack = j;
}
-(void) setMack: (int) m{
mack = m;
}
-(puppy*) waldo: (puppy *) f{
return (f.jack + f.mack); // Error: <-- Accessing unknown "jack" gett开发者_高级运维er method
// Error: <-- Accessing unknown "mack" getter method
}
You have not specified getter method for jack
and mack
. Instead of writing own getter/setter you can use property for them.
@interface puppy : NSObject {
int mack;
int jack;
}
-(puppy *) waldo: (puppy *) f;
// use property
@property (nonatomic, assign) int mack;
@property (nonatomic, assign) int jack;
@end
@implementation puppy
@synthesize jack, mack;
-(puppy*) waldo: (puppy *) f{
return (f.jack + f.mack);
}
@end
You do not need those set methods now. Both getters and setter are synthesized for you. And not asked in the question, you should return int
from method waldo
.
You haven't implemented the methods jack
and mack
.
- (int) jack { return jack; }
- (int) mack { return mack; }
But I'd recommend just using @property and @synthesize with no ivar.
When you do f.jack
, it translates to [f jack]
. You need to add a - (int)jack
method to your interface for this to work. Poorly worded perhaps, I also meant the method needs to be implemented. Same is the case of mack
But that said, dot notation is for properties. Isn't apt.
It would be easier if you defined properties for mack
and jack
and synthesized those methods.
精彩评论